ListItem.onItemClickListener 自定义适配器无响应
ListItem.onItemClickListener with CustomAdapter unresponsive
我有一个自定义的 list_item,它所在的列表视图有一个 onItemClickListener
,可以转到一个新的 Activity。我面临的问题是 onItemClickListener
没有响应,因为它无法识别所单击项目的 ID
。我怀疑这个问题是由我的 CustomAdapter
引起的,但我不确定如何解决它。记录的 ID 列为:
D/ListDataActivity: onItemClick: You Clicked on android.database.sqlite.SQLiteCursor@22fda4b
ListDataActivity.java:
public class ListDataActivity extends AppCompatActivity {
private static final String TAG = "ListDataActivity";
DatabaseHelper mDatabaseHelper;
TextView mTaskName;
TextView mTaskDesc;
ArrayAdapter adapter;
CustomAdapter customAdapter;
private ListView mListView;
DatabaseHelper db = new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
mListView = (ListView) findViewById(R.id.listView);
mDatabaseHelper = new DatabaseHelper(this);
mTaskDesc = (TextView) findViewById(R.id.task_desc);
mTaskName = (TextView) findViewById(R.id.task_name);
populateListView();
}
private void populateListView() {
String[] fromColumns = mDatabaseHelper.databaseToStringArray();
int[] toViews = new int[]{R.id.task_name, R.id.task_desc};
Cursor cursor;
cursor = mDatabaseHelper.getWritableDatabase().rawQuery(" SELECT * FROM " + mDatabaseHelper.TABLE_NAME + " WHERE 1 ", null);
Log.d(TAG, "populateListView: Displaying data in the ListView.");
SimpleCursorAdapter customAdapter = new CustomAdapter(this, R.layout.list_item, cursor, fromColumns, toViews,0);
mListView.setAdapter(customAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String name = adapterView.getItemAtPosition(i).toString();
Log.d(TAG, "onItemClick: You Clicked on " + name);
Cursor data = mDatabaseHelper.getItemID(name);
int priority = 10;
int itemID = -1;
while(data.moveToNext()){
itemID = data.getInt(0);
}
if(itemID > -1){
Log.d(TAG, "onItemClick: The ID is: " + itemID);
Intent editScreenIntent = new Intent(ListDataActivity.this, EditDataActivity.class);
editScreenIntent.putExtra("id",itemID);
editScreenIntent.putExtra("name",name);
editScreenIntent.putExtra("priority",priority);
startActivity(editScreenIntent);
}
else{
toastMessage("No ID associated with that name");
}
}
});
}
private void toastMessage(String message){
Toast.makeText(this,message, Toast.LENGTH_SHORT).show();
}
}
CustomAdapter.java:
public class CustomAdapter extends SimpleCursorAdapter {
private int layout;
private Context context;
private DatabaseHelper mDatabaseHelper;
public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
this.layout = layout;
this.context = context;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Cursor c = getCursor();
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.list_item, parent, false);
bindView(v, context, c);
return v;
}
@Override
public void bindView(View v, Context context, Cursor c) {
int taskNameColumn = c.getColumnIndex(mDatabaseHelper.COL2);
int taskDescColumn = c.getColumnIndex(mDatabaseHelper.COL4);
String mTaskName = c.getString(taskNameColumn);
String mTaskDesc = c.getString(taskDescColumn);
TextView taskName = (TextView) v.findViewById(R.id.task_name);
if (taskName != null){
taskName.setText(mTaskName);
}
TextView taskDesc = (TextView) v.findViewById(R.id.task_desc);
if (taskDesc != null){
taskDesc.setText(mTaskDesc);
}
}
}
这是我的 list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/colorWhite">
<TextView
android:id="@+id/task_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="55dp"
android:layout_marginTop="25dp"
android:text="TextView" />
<View
android:id="@+id/viewbutton"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="21dp"
android:layout_marginTop="24dp"
android:background="@layout/round" />
<TextView
android:id="@+id/task_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/task_name"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="55dp"
android:layout_marginTop="53dp"
android:layout_marginBottom="10dp"
android:focusable="false"
android:text="Undefined"
android:textStyle="bold" />
</RelativeLayout>
如果您希望我对此进行详细说明,我将很乐意。提前致谢!
当您使用 CursorAdapter 时,l(传递给覆盖的 onItemClick 方法的第 4 个参数)将是 id。这假设 _id 列是 rowid 的别名(即您有 _id INTEGER PRIMARY KEY
编码,有或没有 AUTOINCREMENT
).
- NOTE 第4个参数'l',对于其他适配器(如ArrayAdapter)不会id 而不是通常的位置(作为长),因此与第三个参数相同。
这样试试Log.d(TAG, "onItemClick: You Clicked on " + name + " with an id of " + String.valueOf(l));
name 显示为 android.database.sqlite.SQLiteCursor@22fda4b
因为通过 getItem 检索的底层项目是 toString 方法的 Cursor将是打印对象引用的默认继承方法。
你可能想要这样的东西:-
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent editScreenIntent = new Intent(ListDataActivity.this, EditDataActivity.class);
editScreenIntent.putExtra("id",l); //<<<<<<<<<< THE ID AS kindly supplied by the CursorAdapter
editScreenIntent.putExtra("name",data.getString(data.getColumnIndex(mDatabaseHelper.COL2)));
editScreenIntent.putExtra("priority",data.getColumnIndex(mDatabaseHelper.COL4));
startActivity(editScreenIntent);
}
}
});
- 但是,您可以轻松地(并且可以说更正确)通过意图传递 id,然后从调用的 activity 中的 table 检索数据(名称和优先级)。
- 注意以上代码为原理代码,未经过测试,可能存在错误。
- 光标应位于当前 row/respective 行,因此需要进行任何移动。
我有一个自定义的 list_item,它所在的列表视图有一个 onItemClickListener
,可以转到一个新的 Activity。我面临的问题是 onItemClickListener
没有响应,因为它无法识别所单击项目的 ID
。我怀疑这个问题是由我的 CustomAdapter
引起的,但我不确定如何解决它。记录的 ID 列为:
D/ListDataActivity: onItemClick: You Clicked on android.database.sqlite.SQLiteCursor@22fda4b
ListDataActivity.java:
public class ListDataActivity extends AppCompatActivity {
private static final String TAG = "ListDataActivity";
DatabaseHelper mDatabaseHelper;
TextView mTaskName;
TextView mTaskDesc;
ArrayAdapter adapter;
CustomAdapter customAdapter;
private ListView mListView;
DatabaseHelper db = new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
mListView = (ListView) findViewById(R.id.listView);
mDatabaseHelper = new DatabaseHelper(this);
mTaskDesc = (TextView) findViewById(R.id.task_desc);
mTaskName = (TextView) findViewById(R.id.task_name);
populateListView();
}
private void populateListView() {
String[] fromColumns = mDatabaseHelper.databaseToStringArray();
int[] toViews = new int[]{R.id.task_name, R.id.task_desc};
Cursor cursor;
cursor = mDatabaseHelper.getWritableDatabase().rawQuery(" SELECT * FROM " + mDatabaseHelper.TABLE_NAME + " WHERE 1 ", null);
Log.d(TAG, "populateListView: Displaying data in the ListView.");
SimpleCursorAdapter customAdapter = new CustomAdapter(this, R.layout.list_item, cursor, fromColumns, toViews,0);
mListView.setAdapter(customAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String name = adapterView.getItemAtPosition(i).toString();
Log.d(TAG, "onItemClick: You Clicked on " + name);
Cursor data = mDatabaseHelper.getItemID(name);
int priority = 10;
int itemID = -1;
while(data.moveToNext()){
itemID = data.getInt(0);
}
if(itemID > -1){
Log.d(TAG, "onItemClick: The ID is: " + itemID);
Intent editScreenIntent = new Intent(ListDataActivity.this, EditDataActivity.class);
editScreenIntent.putExtra("id",itemID);
editScreenIntent.putExtra("name",name);
editScreenIntent.putExtra("priority",priority);
startActivity(editScreenIntent);
}
else{
toastMessage("No ID associated with that name");
}
}
});
}
private void toastMessage(String message){
Toast.makeText(this,message, Toast.LENGTH_SHORT).show();
}
}
CustomAdapter.java:
public class CustomAdapter extends SimpleCursorAdapter {
private int layout;
private Context context;
private DatabaseHelper mDatabaseHelper;
public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
this.layout = layout;
this.context = context;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Cursor c = getCursor();
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.list_item, parent, false);
bindView(v, context, c);
return v;
}
@Override
public void bindView(View v, Context context, Cursor c) {
int taskNameColumn = c.getColumnIndex(mDatabaseHelper.COL2);
int taskDescColumn = c.getColumnIndex(mDatabaseHelper.COL4);
String mTaskName = c.getString(taskNameColumn);
String mTaskDesc = c.getString(taskDescColumn);
TextView taskName = (TextView) v.findViewById(R.id.task_name);
if (taskName != null){
taskName.setText(mTaskName);
}
TextView taskDesc = (TextView) v.findViewById(R.id.task_desc);
if (taskDesc != null){
taskDesc.setText(mTaskDesc);
}
}
}
这是我的 list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/colorWhite">
<TextView
android:id="@+id/task_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="55dp"
android:layout_marginTop="25dp"
android:text="TextView" />
<View
android:id="@+id/viewbutton"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="21dp"
android:layout_marginTop="24dp"
android:background="@layout/round" />
<TextView
android:id="@+id/task_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/task_name"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="55dp"
android:layout_marginTop="53dp"
android:layout_marginBottom="10dp"
android:focusable="false"
android:text="Undefined"
android:textStyle="bold" />
</RelativeLayout>
如果您希望我对此进行详细说明,我将很乐意。提前致谢!
当您使用 CursorAdapter 时,l(传递给覆盖的 onItemClick 方法的第 4 个参数)将是 id。这假设 _id 列是 rowid 的别名(即您有 _id INTEGER PRIMARY KEY
编码,有或没有 AUTOINCREMENT
).
- NOTE 第4个参数'l',对于其他适配器(如ArrayAdapter)不会id 而不是通常的位置(作为长),因此与第三个参数相同。
这样试试Log.d(TAG, "onItemClick: You Clicked on " + name + " with an id of " + String.valueOf(l));
name 显示为 android.database.sqlite.SQLiteCursor@22fda4b
因为通过 getItem 检索的底层项目是 toString 方法的 Cursor将是打印对象引用的默认继承方法。
你可能想要这样的东西:-
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent editScreenIntent = new Intent(ListDataActivity.this, EditDataActivity.class);
editScreenIntent.putExtra("id",l); //<<<<<<<<<< THE ID AS kindly supplied by the CursorAdapter
editScreenIntent.putExtra("name",data.getString(data.getColumnIndex(mDatabaseHelper.COL2)));
editScreenIntent.putExtra("priority",data.getColumnIndex(mDatabaseHelper.COL4));
startActivity(editScreenIntent);
}
}
});
- 但是,您可以轻松地(并且可以说更正确)通过意图传递 id,然后从调用的 activity 中的 table 检索数据(名称和优先级)。
- 注意以上代码为原理代码,未经过测试,可能存在错误。
- 光标应位于当前 row/respective 行,因此需要进行任何移动。