【发布时间】:2014-06-06 05:41:29
【问题描述】:
我的布局包含多个完美运行的 ListView,但我无法捕获对任何项目的任何点击。
所有 ListViews 上的项目具有相同类型的信息,我打算使用一个适配器在另一个布局上显示该信息。
我已经搜索过,我发现了 2 种好方法,第一种方法当我点击任何项目时什么都不做,第二种方法不让我开始目标活动,我不知道该怎么做更多...
包含 ListView 的布局:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
<TextView
android:id="@+id/day1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical|center_horizontal" />
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="50dp" >
</ListView>
</FrameLayout>
<FrameLayout
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
<TextView
android:id="@+id/day2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical|center_horizontal" />
<ListView
android:id="@+id/list2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="50dp" >
</ListView>
</FrameLayout>
....
....
</LinearLayout>
这是我的第一个代码:
public class MyClass extends Activity implements OnItemClickListener {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Object o = arg0.getAdapter().getItem(arg2);
Intent i = new Intent(MyClass .this, ViewMyClass.class);
startActivity(i);
}
}
当我点击任何项目时,它什么也不做,我不知道为什么..
我的第二个密码:
public class MyClass extends Activity implements AdapterView.OnItemClickListener {
lv1 = (ListView) findViewById (R.id.list1);
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
Object o = arg0.getAdapter().getItem(arg2);
Intent i = new Intent(MyClass .this, ViewMyClass.class);
startActivity(i);
}
});
}
使用此代码,当我单击第一个 ListView 的项目时,不会启动活动 ViewMyClass,也不知道为什么
我也尝试过这样做,但一无所获:
AdapterView.OnItemClickListener with more ListView
ListView onItemClick not working in the Activity
我也尝试过这样做,但如果我用 ListFragment 更改扩展 Activity,几乎我所有有效的代码都会出错...
Android programming - onitemclicklistener for multiple listviews doesn't work
我会继续搜索和尝试,因为我知道它必须是解决此问题的方法,而无需更改所有代码,但如果有人可以提供帮助...
谢谢大家。
【问题讨论】:
-
从第二个代码中删除
implements AdapterView.OnItemClickListener并检查。 -
尽量不要将同一个适配器对象绑定到多个列表视图,而只是创建适配器的两个对象并将一个对象绑定到每个列表视图,数据源可能相同。首先试试这个,让我知道,然后我会让你解释为什么这是必需的。
-
试试这个
private class Myclass implements AdapterView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub switch(parent.getId()) { case R.id.listview1: break; case R.id.listview2: break; } } } -
大家好,我尝试删除 AdapterView.OnClickListener 并只留下第二个代码: public class MyClass extends Activity{ 和 lv1.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView > arg0, 查看 arg1, int arg2, long arg3) { Object o = arg0.getAdapter().getItem(arg2); Intent i = new Intent(MyClass.this, ViewMyClass.class); startActivity(i); } });什么都没有
-
回复 jitain sharma,很抱歉,但我不知道您的意思是创建两个适配器
标签: android listview android-listview onitemclicklistener onitemclick