How to get the selected item from ListView?

匿名 (未验证) 提交于 2019-12-03 08:33:39

问题:

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 adapter=                  new ArrayAdapter(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

回答1:

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


回答2:

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


回答3:

On onItemClick :

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


回答4:

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.



回答5:

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


回答6:

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.



回答7:

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


回答8:

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

You never instantiated your class.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!