Bghaak
Bghaak

Reputation: 321

How to get the selected item from ListView?

in my Android app I have created a ListView component called myList, and filled it with objects of my own custom type:

class MyClass{

    private String displayName;
    private String theValue;
... //here constructor, getters, setters and toString() are implemented

}

I used the ArrayAdapter to bound the ArrayList theObjects with myList:

ArrayAdapter<MyClass> adapter= 
                new ArrayAdapter<MyClass>(this, R.layout.lay_item, theObjects);
myList.setAdapter(adapter);

This works fine, the list is populated and etc., but when I'm trying to access the selected item, i receive a Null object. I've done this using

myList.setOnItemClickListener(new OnItemClickListener() {

   public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {

MyClass selItem = (MyClass) myList.getSelectedItem(); //
String value= selItem.getTheValue(); //getter method

}

What seems to be the problem? Thank you

Upvotes: 32

Views: 199298

Answers (9)

defemz
defemz

Reputation: 581

Though I am using kotlin, the following code answered your question. This return selected item:

val item = myListView.adapter.getItem(i).toString()

The following is the whole selecteditem Listener

myListView.setOnItemClickListener(object : OnItemClickListener {
       override fun onItemClick(parent: AdapterView<*>, view: View, i: Int,
                        id: Long) {
           val item = myListView.adapter.getItem(i).toString()

       }
    })

The code returns the item clicked by its index i as shown in the code

Upvotes: 0

DrTchock
DrTchock

Reputation: 471

By default, when you click on a ListView item it doesn't change its state to "selected". So, when the event fires and you do:

myList.getSelectedItem();

The method doesn't have anything to return. What you have to do is to use the position and obtain the underlying object by doing:

myList.getItemAtPosition(position);

Upvotes: 47

tecnocchio
tecnocchio

Reputation: 41

Using setOnItemClickListener is the correct answer, but if you have a keyboard you can change selection even with arrows (no click is performed), so, you need to implement also setOnItemSelectedListener :

myListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
     MyObject tmp=(MyObject) adapterView.getItemAtPosition(position);
         }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                // your stuff
            }
        });

Upvotes: 3

viper
viper

Reputation: 1906

Since the onItemClickLitener() will itself provide you the index of the selected item, you can simply do a getItemAtPosition(i).toString(). The code snippet is given below :-

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            String s = listView.getItemAtPosition(i).toString();

            Toast.makeText(activity.getApplicationContext(), s, Toast.LENGTH_LONG).show();
            adapter.dismiss(); // If you want to close the adapter
        }
    });

On the method above, the i parameter actually gives you the position of the selected item.

Upvotes: 8

Edward Brey
Edward Brey

Reputation: 41648

In touch mode, there is no focus and no selection. Your UI should use a different type of widget, such as radio buttons, for selection.

The documentation on ListView about this is terrible, just one obscure mention on setSelection.

Upvotes: 3

Bacara
Bacara

Reputation: 7223

On onItemClick :

String text = parent.getItemAtPosition(position).toString();

Upvotes: 6

ngocquynh_183
ngocquynh_183

Reputation: 125

myList.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
      MyClass selItem = (MyClass) adapter.getItem(position);
   }
}

Upvotes: 3

xandy
xandy

Reputation: 27411

You are implementing the Click Handler rather than Select Handler. A List by default doesn't suppose to have selection.

What you should change, in your above example, is to

public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
    MyClass item = (MyClass) adapter.getItem(position);
}

Upvotes: 21

user432209
user432209

Reputation: 20167

MyClass selItem = (MyClass) myList.getSelectedItem(); //

You never instantiated your class.

Upvotes: -1

Related Questions