M
M
MODifikaTOR2017-03-15 15:56:12
Java
MODifikaTOR, 2017-03-15 15:56:12

How to get listview item data?

We need to get data from the ListView (using the system simple_list_item_2 with two TextViews). What should I add to my code?

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Object head = adapter.getItem(position); //насчёт типа переменной не уверен, но компилятор ожидал увидеть здесь Object

            }
        });

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2017-03-15
@MODifikaTOR18

Well just cast

(Map<String, *что у вас там*>)adapter.getItem(position)
And pull out the key from the map.
PS In general, all these Simple/Array adapters are profanity. Make your normal adapter. Better yet, take RecyclerView and do it with it.

S
Sergey Vashchenko, 2017-03-15
@Simipa

ListView is deprecated and not recommended. Use RecyclerView, and inside ViewHolder set listeners to the necessary elements and send events using EventBus. Inside the adapter, write a method that will return the desired element from the array. The position of the element is equal to the position of the object in the array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question