Answer the question
In order to leave comments, you need to log in
Why can't lists be used on Android without adapters?
It has to be twisted like this:
// получаем экземпляр элемента ListView
ListView listView = (ListView)findViewById(R.id.listView);
// определяем массив типа String
final String[] catNames = new String[] {
"Рыжик", "Барсик", "Мурзик"
};
// используем адаптер данных
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, catNames);
listView.setAdapter(adapter);
ListView listView = (ListView)findViewById(R.id.listView);
listView.add("Рыжик");
listView.add("Барсик");
listView.add("Мурзик");
Answer the question
In order to leave comments, you need to log in
Why can't lists be used on Android without adapters?
ListView
it is responsible for laying out the widgets provided to it as a list, and it Adapter
is responsible for creating the supplied widgets. ListView
new widgets, you needArrayAdapter
, since you have two dozen elements that you receive from the network, and they can be stored in memory, and (exaggerating) tomorrow there will be two thousand elements and it’s no longer comme il faut to keep them in memory, so you decide to fasten the caching of elements in the database, and now you need CursorAdapter
. Collection
, and the responsibility to "iterate over the elements" is assigned to the classes implementing the interface Iterator
, although one could dump everything in one heap.
Have you thought about the flexibility of this approach? ListView and RecyclerView are interesting in that they allow you to add an arbitrary View to the list and implement Rx elements.
I think the question is related to trying to use a ListView instead of a Spinner. Each View has its own place.
Other environments have their own ListView counterparts that allow you to use hierarchies as list items.
If added via xml, then the elements will be automatically added as header. In general, there is a ScrollView for such cases, since the main task of the ListView is to reuse elements.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question