E
E
e-hot2019-03-12 19:12:07
Android
e-hot, 2019-03-12 19:12:07

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"
        />
...

in MainActivity:
...
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;
    }
...

Android Studio responds to ringtones in: ArrayAdapter
adapter = ArrayAdapter.createFromResource(this, ringtones , android.R.layout.simple_list_item_single_choice) of the onCreate( ... ) method for ringtones or is it necessary to collect and form an array of ringtones in the public static List getRingtones(Context context) method in some other way? Thank you.


Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2019-03-13
@zagayevskiy

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 question

Ask a Question

731 491 924 answers to any question