【问题标题】:How to make onItemClick work with customCursorAdapter如何使 onItemClick 与 customCursorAdapter 一起使用
【发布时间】:2013-06-19 21:32:53
【问题描述】:

我无法让 clickOnItem 从一个由 customCursorAdapter 填充的列表中工作。我已阅读所有设计文档以及与此和相关领域相关的帖子,但所有尝试都失败了。请帮忙!

列表填充良好,但是当我单击列表项时,我收到错误“没有此类方法异常”onItemClick [class android.view.View]

我正在使用以下代码从数据库查询中输出一组数据:

import android.app.Activity;
import android.app.ListActivity;
import android.database.SQLException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListTripsActivity extends Activity
{

    private CustomCursorAdapter customAdapter;
    private DbHelper databaseHelper;
    private static final int ENTER_DATA_REQUEST_CODE = 1;
    private ListView listView;     
    private static final String TAG = ListTripsActivity.class.getSimpleName();

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);  
        databaseHelper = new DbHelper(this);                       
        customAdapter = new CustomCursorAdapter(this, databaseHelper.getTripList(DbHelper.DESC), 0);    
        listView = (ListView) findViewById(R.id.list); 
        listView.setAdapter(customAdapter); 

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d(TAG, "clicked on item: " + position);
            }
        });  
    }

我的 activity_list.xml 是:

 <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical"
              android:descendantFocusability="blocksDescendants"
               android:theme="@style/AppTheme" >

     <!-- This ListView is populated by customCursorAdapter -->
     <!-- The content layout is defined by row_layout.xml   -->

    <ListView android:id="@+id/list"
              android:layout_height="match_parent"
              android:layout_width="match_parent"              
              android:layout_weight="1"/>

    <Button android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:layout_marginTop="5dp"
            android:text="Change Name"
            android:onClick="onClickEnterData"/>

    </LinearLayout>

row_layout.xml 紧随其后,它是我尝试单击的 2 个 TextView 项。

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical" >

   <EditText  android:id="@+id/activityId"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:textSize="12sp"
              android:paddingBottom="5dp" />

    <EditText android:id="@+id/activityName"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:inputType="text"
              android:textSize="12sp"
              android:paddingBottom="5dp"
              android:imeOptions="actionSend" />

    <TextView android:id="@+id/startDate"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:onClick="onItemClick"                
              android:clickable="true"  
              android:textSize="12sp"
              android:paddingBottom="5dp" />

    <TextView android:id="@+id/endDate"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:onClick="onItemClick"                
              android:clickable="true"
              android:textSize="12sp"
              android:paddingBottom="5dp" />

    </LinearLayout>

非常感谢您的帮助!

【问题讨论】:

  • listview.setAdapter(customAdapter) 声明在哪里。

标签: android android-cursoradapter onitemclick


【解决方案1】:

@Raghunandan 是对的。您从他们使用 ListActivity 并且他们使用 setAdapter 将适配器分配给 listView 的一些示例中获取了此代码。

代替

setAdapter(customAdapter); 

使用

listview.setAdapter(customAdapter)

【讨论】:

  • 我做出了您和@Raghunandan 同意的更改,但仍然出现相同的错误
  • 尝试从行布局xml中删除以下属性:android:onClick="onItemClick" android:clickable="true"
  • 当我删除这些 XML 行中的一个或两个时,单击该项目没有界面响应
  • 如果我的理解正确,您想为列表项添加 onClickListener,而不是为该项目中的特定元素添加。我说的对吗?
  • 是的,没错。如果我可以将它用于我的方法: public void onItemClick(AdapterView> parent, View view, int position, long id) ,参数将告诉我点击了什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-02
  • 2016-08-27
  • 2021-04-24
  • 2016-05-29
  • 1970-01-01
  • 2016-08-30
  • 2021-01-28
相关资源
最近更新 更多