如何使用文本视图值查询数据库行等于文本视图值?

时间:2012-02-06 19:31:32

标签: android database text view row

我很高兴这个网站在这里。它每次都帮助我。

所以我的新问题是,我已将一个值传递给另一个活动,并将该值设置为文本视图。该值始终为数字。然后,我使用“on menu item selected”将该值传递给另一个活动,并将该值设置为文本视图。我想要做的是使用该值(数字并表示与数据关联的行ID)来查询数据库并返回与前面提到的数值相关联的行。然后填充相同活动的相应编辑文本或文本视图。

这是我调用文本值(tv1)

的地方
private void populateFields() {

if (tv1!=null) {
    Cursor note = mDbHelper.fetchRow(tv1);
    startManagingCursor(note);

    //rowid.setText(note.getString(
        //  note.getColumnIndexOrThrow(DatabaseManager.KEY_ROWID)));
    txtbox28.setText(note.getString(
           note.getColumnIndexOrThrow(DatabaseManager.KEY_NAME)));
    txtbox1.setText(note.getString(
            note.getColumnIndexOrThrow(DatabaseManager.NICBASEML)));
    txtbox4.setText(note.getString(
            note.getColumnIndexOrThrow(DatabaseManager.NICDROPS)));
    txtbox8.setText(note.getString(
            note.getColumnIndexOrThrow(DatabaseManager.NICPERCT)));

和查询

public Cursor fetchRow(TextView tv1) throws SQLException {

            Cursor mCursor =

                mDb.query(true,DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
                        NICBASEML, NICDROPS, NICPERCT, PGML, PGDROPS,PGPERCT,
                        VGML,VGDROPS,VGPERCT, WAPML, WAPDROPS, WAPPERCT, 
                        FLV1NAME, FLV1ML, FLV1DROPS, FLV1PERCT,
                        FLV2NAME, FLV2ML, FLV2DROPS, FLV2PERCT,
                        FLV3NAME, FLV3ML, FLV3DROPS, FLV3PERCT,
                        FLV4NAME, FLV4ML, FLV4DROPS, FLV4PERCT,
                        DROPSML, AMOUNT, FINALNIC, BPG, BVG, BASENIC}, KEY_ROWID + "=" + tv1,  null, null, null, null, null);

            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;

        }

这是我现在得到的错误。

02-07 12:14:16.120:E / AndroidRuntime(2713):引起:android.database.sqlite.SQLiteException:在“@ 405dcd48”附近:语法错误:,编译时:SELECT DISTINCT _id,name,nicbaseml ,nicdrops,nicperct,pgml,pgdrops,pgperct,vgml,vgdrops,vgperct,wapml,wapdrops,wapperct,flv1name,flv1ml,flv1drops,flv1perct,flv2name,flv2ml,flv2drops,flv2perct,flv3name,flv3ml,flv3drops,flv3perct,flv4name,flv4ml ,flv4drops,flv4perct,dropsml,amount,finalnic,bpg,bpg,basenic FROM食谱WHERE _id=android.widget.TextView@405dcd48

嗯,坚持这个。误差在哪里@ 405dcd48?

1 个答案:

答案 0 :(得分:0)

这是解决方案。花了很多小时(2天)来计算出来然后它就是一个dhooo!一蹴而就。但从来没有,这是其他具有相同问题的解决方案。在填充字段的声明中(在本例中为fetch(row)。

这样做:

private void populateFields() {

String row = tv1.getText().toString();//String to get the textview of the rowid

        if (tv1!= null) { //Checking to see if the field is not empty to proceed

            Cursor note = mDbHelper.fetchRow(row);//Calling my cursor to manage data
            startManagingCursor(note);//Starting the management and retrieve data

您将得到上述错误,然后是空指针异常。这可以通过声明字段来修复 - >(txtbox1 =(TextView)findViewById(R.id.textView22);.

您的数据库管理器需要配置为String。

像这样:

public Cursor fetchRow(String row) throws SQLException {

现在,您可以使用textview或editview值从行中检索数据,该值等于您要查找的数据的行ID。