从ListView获取ID数据库

时间:2016-04-19 15:40:40

标签: android listview

我有一个问题,需要帮助。

我有一个Activity SearchNote,我在表格中显示数据。

这是我的活动搜索注释

private void addDataListViewSearchByNote(){
        Bundle extras = getIntent().getExtras();
        String idUser = extras.getString("IdUser");
        Cursor cursor = db.getDataTableNoteWithDateAndType(Integer.parseInt(idUser), et.getText().toString(), SelectTypeNote.getSelectedItem().toString());

        if(!(cursor.moveToFirst()) || cursor.getCount() ==0){
            Toast.makeText(SearchNote.this, "Không tìm thấy dữ liệu", Toast.LENGTH_LONG).show();
        }

        String form[] = new String []{db.KEY_CONTENT_NOTE, db.KEY_TYPE_NOTE, db.KEY_DATE_CREATE_NOTE, db.KEY_MONEY};
        int to[] = new int[] {R.id.textView_ContentNote, R.id.textView_TypeNote, R.id.textView_DateCreateNote, R.id.textView_MoneyNote};

        dataAdapter = new SimpleCursorAdapter(this, R.layout.item_note, cursor, form, to, 0);
        ListView listViewManagePage = (ListView) findViewById(R.id.listView_SearchByNote);
        listViewManagePage.setAdapter(dataAdapter);
    }

这是DatabaseHelper中的方法

public Cursor getDataTableNoteWithDateAndType(int id, String date, String Type_Select){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = null;
    int temp = 0;
    if(date.equals("") == true){
        if (Type_Select.equals("Tất cả") == true){
            temp = 1;
        }else{
            temp = 2;
        }
    } else {
        if (Type_Select.equals("Tất cả") == true){
            temp = 3;
        }else{
            temp = 4;
        }
    }
    if(temp ==1){
            res = db.rawQuery("SELECT * FROM " + TABLE_NOTE + " WHERE " + KEY_ID_USER + " = " + id, null);
    }else if (temp ==2){
            res = db.rawQuery("SELECT * FROM " + TABLE_NOTE + " WHERE " + KEY_ID_USER + " = " + id + " AND " + KEY_TYPE_NOTE + " = " + "'" + Type_Select + "'", null);
    }else if (temp == 3 ){
            res = db.rawQuery("SELECT * FROM " + TABLE_NOTE + " WHERE " + KEY_ID_USER + " = " + id + " AND " + KEY_DATE_CREATE_NOTE + " = " + "'" + date + "'", null);
    }else if (temp == 4){
            res = db.rawQuery("SELECT * FROM " + TABLE_NOTE + " WHERE " + KEY_ID_USER + " = " + id + " AND " + KEY_TYPE_NOTE + " = " + "'" + Type_Select + "'"
                    + " AND " + KEY_DATE_CREATE_NOTE + " = " + "'" + date + "'", null);
    }
    return res;
}

我在ListView上显示的Spinner和Data属于Spinner。 当我点击它时,我正试图获取项目的ID。

我的表TABLE_NOTE有一个列名“Type_Note”。当我选择Type in Spinner时,我用它在Listview上显示数据。

当我选择“Tấtcả”时,所有数据都将显示在ListView中,因此我可以获得项目的令人兴奋的ID。 但是当我选择“Thu”或“Chi”时,项目的ID不是真的。

那么当我在ListView上显示数据库的一部分时,如何在TABLE_NOTE上获取项目的_id。

对不起我的英语,这不好

这是方法ContextMenu

public boolean onContextItemSelected(MenuItem item){
    Bundle extras = getIntent().getExtras();
    final String idUser = extras.getString("IdUser");
    AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    AdapterView.AdapterContextMenuInfo info= (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

    int index = info.position;


    if(item.getTitle()=="Chỉnh sửa ghi chép"){
        Toast.makeText(getApplicationContext(),"Chỉnh sửa ghi chép",Toast.LENGTH_LONG).show();
    }
    else if(item.getTitle()=="Xóa ghi chép"){
        Toast.makeText(getApplicationContext(), Integer.toString(index) ,Toast.LENGTH_LONG).show();
    }else if(item.getTitle()=="Đóng"){
        doOpenSearchByNote(idUser);
    }
    return true;
}

0 个答案:

没有答案