88import android .content .DialogInterface ;
99import android .content .Intent ;
1010import android .content .pm .PackageManager ;
11+ import android .content .res .TypedArray ;
1112import android .database .Cursor ;
1213import android .database .MatrixCursor ;
1314import android .database .MergeCursor ;
3031import android .util .Log ;
3132import android .view .LayoutInflater ;
3233import android .view .View ;
34+ import android .view .ViewGroup ;
3335import android .webkit .MimeTypeMap ;
36+ import android .widget .Adapter ;
37+ import android .widget .ArrayAdapter ;
38+ import android .widget .CheckedTextView ;
3439import android .widget .CursorAdapter ;
3540import android .widget .HeaderViewListAdapter ;
41+ import android .widget .ListAdapter ;
3642import android .widget .ListView ;
43+ import android .widget .SimpleCursorAdapter ;
3744import android .widget .Toast ;
3845
3946import com .takisoft .fix .support .v7 .preference .ringtone .R ;
4653import java .io .InputStream ;
4754import java .io .OutputStream ;
4855import java .security .SecureRandom ;
56+ import java .util .ArrayList ;
4957import java .util .concurrent .LinkedBlockingQueue ;
58+ import java .util .zip .Inflater ;
5059
5160import static android .app .Activity .RESULT_OK ;
5261
@@ -105,8 +114,6 @@ protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
105114
106115 createCursor (ringtonePreference .getRingtone ());
107116
108- String colTitle = cursor .getColumnName (RingtoneManager .TITLE_COLUMN_INDEX );
109-
110117 final Context context = getContext ();
111118
112119 final int ringtoneType = ringtonePreference .getRingtoneType ();
@@ -119,9 +126,16 @@ protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
119126 } else {
120127 defaultUri = null ;
121128 }
129+
130+ String [] titles = new String [cursor .getCount ()];
131+ if (cursor .moveToFirst ()){
132+ do {
133+ titles [cursor .getPosition ()] = cursor .getString (RingtoneManager .TITLE_COLUMN_INDEX );
134+ }while (cursor .moveToNext ());
135+ }
122136
123137 builder
124- .setSingleChoiceItems (cursor , selectedIndex , colTitle , new DialogInterface .OnClickListener () {
138+ .setSingleChoiceItems (buildAdapter ( context , cursor ), selectedIndex , new DialogInterface .OnClickListener () {
125139 @ Override
126140 public void onClick (DialogInterface dialogInterface , int i ) {
127141 if (i < cursor .getCount ()) {
@@ -292,8 +306,7 @@ protected Cursor doInBackground(Uri... params) {
292306 protected void onPostExecute (final Cursor newCursor ) {
293307 if (newCursor != null ) {
294308 final ListView listView = ((AlertDialog ) getDialog ()).getListView ();
295- final CursorAdapter adapter = ((CursorAdapter ) ((HeaderViewListAdapter ) listView .getAdapter ()).getWrappedAdapter ());
296- adapter .changeCursor (newCursor );
309+ listView .setAdapter (buildAdapter (context , newCursor ));
297310
298311 listView .setItemChecked (selectedIndex , true );
299312 listView .setSelection (selectedIndex );
@@ -532,6 +545,20 @@ public static String[] splitFileName(String mimeType, String displayName) {
532545 return new String []{name , ext };
533546 }
534547
548+ private CheckedItemAdapter buildAdapter (Context context , Cursor cursor ){
549+ String [] titles = new String [cursor .getCount ()];
550+ if (cursor .moveToFirst ()){
551+ do {
552+ titles [cursor .getPosition ()] = cursor .getString (RingtoneManager .TITLE_COLUMN_INDEX );
553+ }while (cursor .moveToNext ());
554+ }
555+
556+ final TypedArray a = context .obtainStyledAttributes (null , android .support .v7 .appcompat .R .styleable .AlertDialog ,
557+ android .support .v7 .appcompat .R .attr .alertDialogStyle , 0 );
558+ int layout = a .getResourceId (android .support .v7 .appcompat .R .styleable .AlertDialog_singleChoiceItemLayout , 0 );
559+ return new CheckedItemAdapter (context , layout , android .R .id .text1 , titles );
560+ }
561+
535562 /**
536563 * Creates a {@link android.media.MediaScannerConnection} to scan a ringtone file and add its
537564 * information to the internal database.
@@ -579,4 +606,21 @@ private Uri take() throws InterruptedException {
579606 return mQueue .take ();
580607 }
581608 }
609+
610+ private static class CheckedItemAdapter extends ArrayAdapter <CharSequence > {
611+ public CheckedItemAdapter (Context context , int resource , int textViewResourceId ,
612+ CharSequence [] objects ) {
613+ super (context , resource , textViewResourceId , objects );
614+ }
615+
616+ @ Override
617+ public boolean hasStableIds () {
618+ return true ;
619+ }
620+
621+ @ Override
622+ public long getItemId (int position ) {
623+ return position ;
624+ }
625+ }
582626}
0 commit comments