ListItem.onItemClickListener与CustomAdapter无响应

时间:2018-10-18 21:39:02

标签: java listview android-sqlite simplecursoradapter

我有一个自定义的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);
    }
}

如果您希望我对此进行详细说明,我将很乐意。预先感谢!

1 个答案:

答案 0 :(得分:1)

当您使用CursorAdapter时, l (传递给覆盖的 onItemClick 方法的第四个参数)将是 id 。假设 _id 列是 rowid 的别名(即,您使用_id INTEGER PRIMARY KEY编码,带有或不带有AUTOINCREMENT)。

  • 注意 第四个参数'l',对于其他适配器(例如ArrayAdapter)将不是 id 而是通常是位置(长),因此与第三个参数相同。

这样尝试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);
        }
    }
});
  • 但是,您可以轻松地(并且可以说是更正确地)通过意图传递ID,然后从被调用的活动表中检索数据(名称和优先级)。
  • 请注意,以上代码是内部代码,未经测试,因此可能包含错误。
  • 光标应位于当前行/相应行,因此需要进行任何移动。