onItemClick不被调用上ListActivity [英] onItemClick not being invoked on ListActivity
问题描述
我有一个类从 ListActivity
延伸并实现了 OnItemClickListener
I have a class that extends from ListActivity
and implements OnItemClickListener
这是一个非常简单的测试类,这个想法是,我在列表中选择一个项目,它显示了在选定项目吐司
。
It's a very simple test class, the idea is that I select an item on the list, and it shows the selected item on a Toast
.
我可以看到列表通常在模拟器上,我也可以看到单击该项目的影响,但没有任何反应。
I can see the list normally on the emulator, and I can also see the effects of clicking in the item, but then nothing happens.
我不认为该事件被解雇了,因为我看到LogCat中什么都没有,这里的code:
I don't think the event is being fired, because I see nothing on LogCat, here's the code:
public class CarsListActivity extends ListActivity implements
OnItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listCars()));
}
private List<String> listCars() {
return Arrays.asList("Ferrari", "Lamborghini", "Porsche");
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = (TextView) view;
String message = "Selected car: " + textView.getText();
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
的 Activitiy
的定义是这样的的AndroidManifest.xml
文件:
The Activitiy
is defined like this on the AndroidManifest.xml
file:
<activity android:name=".CarsListActivity" />
有什么我失踪?
我研究这个错误,我发现很多的解决方案说,这涉及可点击和布局可聚焦性属性。但我使用Android的自身 android.R.layout.simple_list_item_1
,所以我真的不知道我怎么能解决这个问题。
I researched this error and I found many solutions saying that this concerns clickability and focusability attributes on the layout. But I'm using Android's own android.R.layout.simple_list_item_1
so I don't really know how I could fix it.
我缺少一些配置?
推荐答案
您需要注册OnItemClickListener(活动)是这样的:
You need to register the OnItemClickListener (the activity) like this :
getListView().setOnItemClickListener(this)
只需实现OnItemClickListener接口不足以
Simply implementing the OnItemClickListener interface is not sufficient
这篇关于onItemClick不被调用上ListActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!