【问题标题】:ListView OnItemClickListener not WorkingListView OnItemClickListener 不工作
【发布时间】:2014-02-24 07:34:44
【问题描述】:

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

行.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 上是否有任何 ClickListener?
  • 是的,点击复选框。
  • 您为此定义了SetOnClickListener
  • 在 xml 中为您的复选框添加这两行
  • android:focusable="false" android:focusableInTouchMode="false"

标签: android listview onitemclicklistener


【解决方案1】:

把这个写在你的xml里面的checkbox标签里

android:focusable="false"

使用这个后试试..

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 他做到了。 Lv 它的名字大概是 Lv.setOnItemClickListener(new OnItemClickListener()
    • 谢谢你,我找到了解决方案.. 但我也会尝试你的解决方案。
    【解决方案3】:

    由于行中有一个复选框,ListView OnitemClickListener 将无法工作。可以在 (Adapter) 行中写 OnItemClickLister 来获取点击事件。

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-26
        • 1970-01-01
        相关资源
        最近更新 更多