如何使onItemClick与customCursorAdapter一起使用

时间:2013-06-19 21:32:54

标签: android android-cursoradapter onitemclick

我无法使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>

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

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

而不是

setAdapter(customAdapter); 

使用

listview.setAdapter(customAdapter)