我有一个自定义的list_item,它所在的listview中有一个service cloud.firestore {
match /database {
match /{userId} {
allow read, write;
}
}
}
,可以转到一个新的Activity。我面临的问题是onItemClickListener
无法响应,因为它无法识别单击的项目的onItemClickListener
。我怀疑这个问题是由我的ID
引起的,但是我不确定该如何解决。记录的ID列为:
D / ListDataActivity:onItemClick:您点击了android.database.sqlite.SQLiteCursor@22fda4b
ListDataActivity.java:
CustomAdapter
}
CustomAdapter.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();
}
}
这是我的list_item.xml
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);
}
}
如果您希望我对此进行详细说明,我将很乐意。预先感谢!
答案 0 :(得分:1)
当您使用CursorAdapter时, l (传递给覆盖的 onItemClick 方法的第四个参数)将是 id 。假设 _id 列是 rowid 的别名(即,您使用_id INTEGER PRIMARY KEY
编码,带有或不带有AUTOINCREMENT
)。
这样尝试Log.d(TAG, "onItemClick: You Clicked on " + name + " with an id of " + String.valueOf(l));
名称显示为android.database.sqlite.SQLiteCursor@22fda4b
,因为通过 getItem 检索的下标项目是Cursor,其 toString 方法将是打印的默认继承方法对对象的引用。
您可能想要类似:-
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);
}
}
});