ListView OnItemClickListener无法正常工作

时间:2014-02-24 07:34:45

标签: android listview onitemclicklistener

我使用listView创建统计信息活动。但onItemClick不工作我厌倦了互联网,但解决方案不适合我。

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");    
    }
});

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>

我试了很多但没有工作帮我解决这个问题

4 个答案:

答案 0 :(得分:3)

将此内容写入xml里面的复选框标记

android:focusable="false"

尝试使用此功能后..

答案 1 :(得分:1)

如果您的MyCustomAdapter实现了OnItemClickListener,那么您必须使用以下代码:      MyCustomAdapter.setOnItemclikListener(DataAdapter的);

答案 2 :(得分:1)

由于行中有一个复选框,ListView OnitemClickListener将无效。您可以在行(Adapter)中编写OnItemClickListener来获取click事件。

答案 3 :(得分:1)

如果在日志中添加args2会返回点击位置吗?

Log.d("click", "Row " + args.toString);