Android ListView侦听器不工作
我使用listView创建了一个统计活动。但McClick不工作我上网,但给出的解决方案不适合我Android ListView侦听器不工作,android,listview,onitemclicklistener,Android,Listview,Onitemclicklistener,我使用listView创建了一个统计活动。但McClick不工作我上网,但给出的解决方案不适合我 try { int counter=0; db = helper.getReadableDatabase(); c = db.query(DBHelper.Result, null, null, null, null, null, null); c.moveToFirst(); do { counter++; State
try
{
int counter=0;
db = helper.getReadableDatabase();
c = db.query(DBHelper.Result, null, null, null, null, null, null);
c.moveToFirst();
do
{
counter++;
States state = new States(c.getString(2), c.getString(3), false);
stateList.add(state);
}while(c.moveToNext());
Log.d("counter", ""+counter);
/*adapter = new SimpleCursorAdapter(this, R.layout.row, c, new String [] {DBHelper.R_test,DBHelper.R_testNM}, new int []{R.id.rowTxt1,R.id.rowTxt2});
Lv.setAdapter(adapter);
Lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);*/
db.close();
}
catch(Exception e)
{
e.printStackTrace();
Log.d("Error", ""+e.getMessage());
}
// create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this, R.layout.row, stateList);
//ListView listView = (ListView) findViewById(R.id.LvStatistics);
// Assign adapter to ListView
Lv.setAdapter(dataAdapter);
Lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
Log.d("click", "0");
}
});
试试看
{
int计数器=0;
db=helper.getReadableDatabase();
c=db.query(DBHelper.Result,null,null,null,null,null,null);
c、 moveToFirst();
做
{
计数器++;
States state=新状态(c.getString(2),c.getString(3),false);
stateList.add(state);
}而(c.moveToNext());
Log.d(“计数器”,“计数器+计数器”);
/*adapter=new SimpleCursorAdapter(this,R.layout.row,c,新字符串[]{DBHelper.R_test,DBHelper.R_testNM},新int[]{R.id.rowTxt1,R.id.rowTxt2});
低压设置适配器(适配器);
Lv.setChoiceMode(ListView.CHOICE\u MODE\u MULTIPLE)*/
db.close();
}
捕获(例外e)
{
e、 printStackTrace();
Log.d(“错误”,“e.getMessage());
}
//从字符串数组创建ArrayAdaptar
dataAdapter=新的MyCustomAdapter(this,R.layout.row,stateList);
//ListView ListView=(ListView)findViewById(R.id.LvStatistics);
//将适配器分配给ListView
Lv.setAdapter(数据适配器);
Lv.setOnItemClickListener(新的OnItemClickListener()
{
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3)
{
Log.d(“单击”、“0”);
}
});
row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/Background"
android:orientation="vertical" >
<TextView
android:id="@+id/rowTxt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/ExamFontColor"
/>
<TextView
android:id="@+id/rowTxt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/rowTxt1"
android:layout_marginLeft="20dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/FontBlack"
/>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:text="1" />
</RelativeLayout>
exam_statistics.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/Background"
android:orientation="vertical" >
<TextView
android:id="@+id/statisticsHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@color/HeaderColor"
android:gravity="center"
android:text="@string/ButtonStatistics"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/FontWhite"
android:textSize="30sp" />
<ListView
android:id="@+id/LvStatistics"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/btnDelete"
android:background="@color/Background"
>
</ListView>
<Button
android:id="@+id/btnDelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/statisticsHeader"
android:layout_marginBottom="10dp"
android:background="@drawable/button_green_effect"
android:text="@string/ButtonDelete"
android:textColor="@color/FontWhite"
android:textSize="27sp" />
</RelativeLayout>
我尝试了很多,但没有成功。如果MyCustomAdapter实现了OnItemClickListener,请帮助我解决此问题,因此您必须使用以下代码:
setOnItemclikListener(dataAdapter) 如果MyCustomAdapter实现了OnItemClickListener,则必须使用以下代码:
setOnItemclikListener(dataAdapter) 在复选框标记内的ur xml中写入此内容
android:focusable="false"
使用此项后再试。在复选框标记内的xml中写入此项
android:focusable="false"
请在使用后重试。因为行中有一个复选框,ListView和McClickListener将无法工作。您可以在行(适配器)中写入OnItemClickLister以获取单击事件。由于行中有复选框,ListView OnitemClickListener无法工作。您可以在行(适配器)中写入OnItemClickLister以获取单击事件。如果在日志中添加args2,它是否返回单击位置
Log.d("click", "Row " + args.toString);
如果在日志中添加args2,它是否返回单击位置
Log.d("click", "Row " + args.toString);
您是否在
MyCustomAdapter
上有任何ClickListener?是的,单击复选框。您为此定义了SetOnClickListener
?在xmlandroid:focusable=“false”android:focusableInTouchMode=“false”中为您的复选框添加这两行您是否在MyCustomAdapter
上有任何ClickListener?是的,单击复选框。您为此定义了SetOnClickListener
?在xmlandroid:focusable=“false”android:focusableInTouchMode=“false”中为您的复选框添加这两行。Lv的名字可能是Lv.setonicmclicklistener(new OnItemClickListener())谢谢你我找到了解决方案。但是我也会尝试你的解决方案。他会的。Lv的名字可能是Lv.setonicmclicklistener(new OnItemClickListener())谢谢你我找到了解决方案。但是我也会尝试你的解决方案。