我一直无法从customCursorAdapter填充的列表中获得clickOnItem工作。我已经阅读了所有的设计文档,以及与此和相关领域相关的帖子,但所有的尝试都失败了。请帮助!
列表填充良好,但当我点击一个列表项目,我得到错误"No such method exception" 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>
非常感谢你的帮助! @Raghunandan是对的。你把这个代码从一些例子,他们使用ListActivity和他们使用setAdapter分配适配器到listView。
代替
setAdapter(customAdapter);
使用listview.setAdapter(customAdapter)