ListView控件onItemClick不在活动工作 [英] ListView onItemClick not working in the Activity
问题描述
当我在里面有ListView中使用的活动这一点,也没有工作。
usersList.setOnItemClickListener(新AdapterView.OnItemClickListener(){ 公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,
翁ARG3){
尝试{
意向意图=新意图(UsersListActivity.this,
UserActivity.class);
startActivity(意向);
}赶上(例外五){
e.printStackTrace();
}
}
});
寻找一个解决方案,我结束了在的 getView()
这件事以后在适配器
convertView.setOnItemClickListener(新AdapterView.OnItemClickListener(){ 公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,
翁ARG3){
尝试{
意向意图=新意图(mActivity,
UserActivity.class);
mActivity.startActivity(意向);
}赶上(例外五){
e.printStackTrace();
}
}
});
这工作。但是,为什么这样奇怪的行为。为什么没有它的工作应该的方式。
感谢您
usersList.setOnItemClickListener(新
AdapterView.OnItemClickListener(){
块引用>在这里,你已经使用
这里查看
的适配器查看
,这是父类的自定义适配器
。这就是为什么它不工作。相反,你应该已经使用查看
的自定义适配器
。
getView()
功能可帮助您父Adpater查看
的视图转换为自定义适配器
查看。所以,您使用的两个选项,
usersList.setOnItemClickListener(新CustomAdapterView.OnItemClickListener(){
或
usersList.setOnItemClickListener(新OnItemClickListener(){
这两种选项可能会奏效。
When i am using this in the activity which has the ListView, it didn't work.
usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, ong arg3) { try { Intent intent = new Intent(UsersListActivity.this, UserActivity.class); startActivity(intent); } catch (Exception e) { e.printStackTrace(); } } });
after searching for a solution i ended up doing it in the
getView()
of the AdapterconvertView.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, ong arg3) { try { Intent intent = new Intent(mActivity, UserActivity.class); mActivity.startActivity(intent); } catch (Exception e) { e.printStackTrace(); } } });
This works. But why such strange behavior. Why isn't it working the way it should.
Thank You
解决方案
usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
Here you have used
View
ofAdapter View
, which is the parent class of yourCustom Adapter
. that's why it is not working. Instead you should have used theView
ofCustom Adapter
.
getView()
function here helps you to convert the View of ParentAdpater View
to theCustom Adapter
View.So you use either of the two options,
usersList.setOnItemClickListener(new CustomAdapterView.OnItemClickListener() {
or
usersList.setOnItemClickListener(new OnItemClickListener() {
Both of the options might work.
这篇关于ListView控件onItemClick不在活动工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!