无法在另一个活动中获取listview项ID

时间:2015-02-01 11:03:08

标签: android database sqlite listview

点按listview项后,它应该将项ID 发送到另一个活动。

我的情况如下,它在

处显示错误
Cursor cursor = trDb.getReminder(value);

,错误是

Incompatible type.
    Required: android.database.Cursor;
    Found:    com.android.app.TRListFormat

TRList.class

的代码段
lv = (ListView)findViewById(R.id.tbr_list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            Intent intent = new Intent(TRList.this, TRTimeReminder.class);
            intent.putExtra("remId", (int)id);
            startActivity(intent);
        }
    });

TRTimeReminder.class

的代码段
Bundle extras = getIntent().getExtras();
    if(extras != null){
        int value = extras.getInt("remId");
        if(value > 0){
            Cursor cursor = trDb.getReminder(value);  //this line is red showing the error I have written above
            id_To_Update = value;
            cursor.moveToFirst();

            String title = cursor.getString(cursor.getColumnIndex(TRDBHelper.COLUMN_TITLE));
            String des = cursor.getString(cursor.getColumnIndex(TRDBHelper.COLUMN_DES));
            String date = cursor.getString(cursor.getColumnIndex(TRDBHelper.COLUMN_DATE));
            String time = cursor.getString(cursor.getColumnIndex(TRDBHelper.COLUMN_TIME));
            if (!cursor.isClosed()) cursor.close();

            edTitle.setText(title);
            edDes.setText(des);
            dateView.setText(date);
            timeView.setText(time);
        }
    }
getReminder

TRDBHelper.class方法

TRListFormat getReminder(int id){

    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_NAME, new String[]{COLUMN_ID, COLUMN_TITLE, COLUMN_DES,
                            COLUMN_DATE, COLUMN_TIME}, COLUMN_ID + "=?",
                            new String[]{String.valueOf(id)}, null, null, null, null);
    if(cursor != null){
        cursor.moveToFirst();
    }

    return new TRListFormat(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4));
}

1 个答案:

答案 0 :(得分:1)

下面:

  

TRListFormat getReminder(int id)

getReminder返回类型为TRListFormat,但您尝试将TRListFormat类对象分配给Cursor对象:

TRListFormat objTRListFormat = trDb.getReminder(value);