Android 自定义列表视图,setOnItemSelectedListener 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3630468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android custom listview, setOnItemSelectedListener not working
提问by Tejaswi Yerukalapudi
I'm just beginning Android development, and I'm working to get a Custom listview with a checkbox working. I've created a base class that extends Activity, Created an Adapter and overrode the getView() method to add the checkbox to the listview. I'm assuming I need to do this because I need something equivalent to didSelectRowIndexAtPath
from Obj C to update my model. Please let me know if there's an alternate way of doing this too!
我刚刚开始 Android 开发,我正在努力获得一个带有复选框的自定义列表视图。我创建了一个扩展Activity的基类,创建了一个适配器并覆盖了 getView() 方法以将复选框添加到列表视图。我假设我需要这样做,因为我需要类似于didSelectRowIndexAtPath
Obj C 的东西来更新我的模型。请让我知道是否还有其他方法可以做到这一点!
Now in my base class, I have the following code -
现在在我的基类中,我有以下代码 -
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout);
setContentView(R.layout.facilityscreen);
/* Static Data source */
facilityModel = new FacilityDataModel[2];
facilityModel[0] = new FacilityDataModel();
facilityModel[1] = new FacilityDataModel();
facilityModel[0].setFacilityName("Test 1");
facilityModel[0].setFacilityID("Facid0001");
facilityModel[0].setChecked(false);
facilityModel[1].setFacilityName("Test 2");
facilityModel[1].setFacilityID("Facid0002");
facilityModel[1].setChecked(true);
facilityListView = (ListView) findViewById(R.id.facilityListView);
FacilityScreenAdapter adapter = new FacilityScreenAdapter(this, facilityModel);
facilityListView.setAdapter(adapter);
myPatBtn = (Button) findViewById(R.id.myPatBtn);
myPatBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int i=0;
i++;
}});
facilityListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int i=0;
i++;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
My problem now is the setOnItemSelectedListener isn't getting called at all. Been struggling with this for a couple of hours now, and I can't figure out why it wouldn't get called at all.
我现在的问题是 setOnItemSelectedListener 根本没有被调用。现在已经为此苦苦挣扎了几个小时,我不明白为什么它根本不会被调用。
Any help is much appreciated!
任何帮助深表感谢!
Thanks,
Teja.
谢谢,
泰嘉。
回答by Skourkos
I know this is an outdated answer but I'm going to write it just in case some other fellow who has the same "problem" bumps onto this page :
我知道这是一个过时的答案,但我会写它以防万一其他有相同“问题”的人碰到这个页面:
The solution to the above problem which is not a problem but just a misunderstanding is that the ListView.onItemSelected() event is fired up, upon :
上述问题的解决方案不是问题,而是一个误解,即 ListView.onItemSelected() 事件在以下情况下被触发:
1) Navigating through the emulators-cross handles or 2) as far-as my HTC-Hero is concerned, the rolling-action on the white little roller-ball.
1) 浏览模拟器交叉手柄或 2) 就我的 HTC-Hero 而言,白色小滚球上的滚动动作。
You don't have to extend your activity explicitly to a ListActivity.
您不必将您的活动显式扩展到 ListActivity。
Here's my tiny little code which retrieves a phone number from a TextView control, inside a listview item. When the user either touches the list item or scrolls through the list with
这是我的小代码,它从列表视图项内的 TextView 控件中检索电话号码。当用户触摸列表项或滚动列表时
the little roller-ball the below Events, fire up and MakeACall() method is called :
小滚球下面的事件,启动并调用 MakeACall() 方法:
myList.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long i)
{
TextView myPhone = (TextView)view.findViewById(R.id.txtphone);
MakeACall(myPhone.getText().toString());
}
});
myList.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long i)
{
TextView myPhone = (TextView)view.findViewById(R.id.txtphone);
MakeACall(myPhone.getText().toString());
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
I hope that was helpful... :)
我希望这有帮助... :)
回答by Juri
There exists already the possibility to have a ListView with checkboxes.
已经存在带有复选框的 ListView 的可能性。
public class List11 extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, GENRES));
final ListView listView = getListView();
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
private static final String[] GENRES = new String[] {
"Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
"Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
};
}
I've taken this from the APIDemos'cause it was the simplest. You can then get the selected items by using:
我从APIDemos 中获取了这个,因为它是最简单的。然后,您可以使用以下方法获取所选项目:
long[] selectedIds = getListView().getCheckItemIds();
What you may also be interested in is the CheckedTextViewwhich is used internally in the list.
您可能还对列表中内部使用的CheckedTextView感兴趣。
To the part of the onListItemClick
problem
Try to extend from ListActivity
rather than Activity
. Then override the onListItemClick
. That should work.
到onListItemClick
问题的部分
尝试从而ListActivity
不是扩展Activity
。然后覆盖onListItemClick
. 那应该工作。
回答by maanijou
You should set all focusable items in custom list layout to false:
您应该将自定义列表布局中的所有可聚焦项设置为 false:
android:focusable="false"
also I think you should not use attributes like android:clickable="true" for them.
另外我认为你不应该为它们使用像 android:clickable="true" 这样的属性。
回答by vikas aggarwal
use
用
setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// here you code
}
})
instead of setOnItemSelectedListener
而不是 setOnItemSelectedListener
As setOnItemSelectedListener is called when item is being selected not clicked so to get clicked item you must use setOnItemClickListener this will work
由于在选择项目时调用 setOnItemSelectedListener 未单击,因此要获得单击的项目,您必须使用 setOnItemClickListener 这将起作用
回答by Edward Brey
The lack of the item selected listener getting called is by design and is based on which mode the device is in. In touch mode, there is no focus and no selection. Your UI should use widgets that differentiate between touch for selection versus touch for scrolling. Radio buttons, for example, are good when there is a single selection choice.
缺少 item selected 侦听器被调用是设计使然,并且基于设备所处的模式。在触摸模式下,没有焦点也没有选择。你的 UI 应该使用区分触摸选择和触摸滚动的小部件。例如,单选按钮在只有一个选择项时很好。