S
S
Sergious6662016-06-25 02:00:41
Android
Sergious666, 2016-06-25 02:00:41

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);

And this is ListView, I’m not talking about Recycler at all, I would like even more code
:
ListView listView = (ListView)findViewById(R.id.listView);
listView.add("Рыжик");
listView.add("Барсик");
listView.add("Мурзик");

And the strangest thing is that no one ever asked this question and did not make their own extends ListView with blackjack and whores.
It seems that it is everywhere, Winforms, WPF, Qt, but not here.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Artem Gapchenko, 2016-06-25
@artemgapchenko

Why can't lists be used on Android without adapters?

Because the Single Responsibility Principle, aka the Single Responsibility Principle . Each class should have one single responsibility. In the example you provided, ListViewit is responsible for laying out the widgets provided to it as a list, and it Adapteris responsible for creating the supplied widgets.
Separation of concerns makes classes simpler and less error-prone when changes are made to them. Also, separating the adapter into a separate class allows you to relatively quickly replace its implementations - today, in order to supply your ListViewnew 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.
Based on the same principle, for example, in Java, the responsibility to "store the elements" is assigned to the classes implementing the interface 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.

P
Peter, 2016-06-25
@petermzg

You can add any view without an adapter.
listView.addView(view);

T
Tiberal, 2016-06-25
@Tiberal

Your approach is not extensible

A
Alexander Varakosov, 2016-06-25
@thelongrunsmoke

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.

B
bracadabra, 2016-06-30
@bracadabra

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 question

Ask a Question

731 491 924 answers to any question