0

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

非常感谢您的帮助!

4

1 回答 1

0

@Raghunandan Is right. You took this code from some example where they are using ListActivity and they are using setAdapter to assign adapter to listView.

Instead of

setAdapter(customAdapter); 

use

listview.setAdapter(customAdapter)
于 2013-06-19T22:38:42.497 回答