Answer the question
In order to leave comments, you need to log in
How to form an array of ringtones to display them in simple_list_item_single_choice?
Greetings. We need your help to understand the following situation.
Situation:
I am writing an application ( API 28 ) for Android on Android Studio 3.1 to play a ringtone for an incoming call. Accordingly, in the application itself, the user is trying to display all the ringtones available on the phone first in the list with a radio-selection ( simple_list_item_single_choice.xml ). I picked up various lessons and docks in the internet, but the result is not obtained.
There is:
in activity_main.xml:
...
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_of_ringtones"
/>
...
...
private ListView listOfRingtones;
private List<Ringtone> ringtones;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listOfRingtones = (ListView)findViewById(R.id.list_of_ringtones);
// получаем массив рингтонов
ringtones = getRingtones(this);
// устанавливаем режим выбора пунктов списка
listOfRingtones.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, ringtones,
android.R.layout.simple_list_item_single_choice);
listOfRingtones.setAdapter(adapter);
}
...
public static List<Ringtone> getRingtones(Context context)
{
List<Ringtone> ringtones = new ArrayList<Ringtone>();
RingtoneManager mgr = new RingtoneManager(context);
mgr.setType(RingtoneManager.TYPE_RINGTONE);
int n = mgr.getCursor().getCount();
for(int i=0;i<n;i++){
ringtones.add(mgr.getRingtone(i));
}
return ringtones;
}
...
Answer the question
In order to leave comments, you need to log in
Well, she writes to you in human language that the types do not match. There it is necessary to transfer int, and you transfer the list. Xs, here you just need to take up your mind and start thinking.
To immediately set you on the right path, the recommendations are to throw out the ListView (this is the old, inconvenient, non-obvious working mammoth shit. All these ArrayAdapters are dull shit that needs to be rewritten as soon as you want to do something more complicated).
You take RecyclerView, you take a normal manual, in the adapter code there will be a little more code than you are used to.
Profit.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question