0

我是新来的 :D

我已经在我的 android 应用程序的数据库 sqlite 中检索了一个图像,但是当我尝试获取另一个图像时,图像仍然和以前一样。它总是从第 1 行检索图像,我不知道为什么

这是我的代码

public void findAlphabet(View view) {
    try {
        db = kamusTI.getWritableDatabase();
        String sql = "SELECT * FROM kamusti WHERE istilah LIKE '"
                + editAlphabet.getText().toString() + "%' ORDER BY istilah";
        cursor = db.rawQuery(sql, null);            
        adapter2 = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_2, cursor, new String[] {
                        "istilah", "definisi" }, new int[] {
                        android.R.id.text1, android.R.id.text2 });
        listView2.setAdapter(adapter2);
        listView2.setTextFilterEnabled(true);
        String sql1 = "SELECT COUNT(istilah) FROM kamusti WHERE istilah LIKE '"
                + editAlphabet.getText().toString() + "%' ORDER BY istilah";
        cursor = db.rawQuery(sql1, null);
        cursor.moveToFirst();

        cur = db.query("kamusti", null, null, null, null, null, null);
        cur.moveToFirst();
    //  cur.moveToPosition(0);
        byte[] im = cur.getBlob(3);
        //  ByteArrayInputStream imagestream = new ByteArrayInputStream(im);
            Bitmap imagestream = BitmapFactory.decodeByteArray(im, 0, im.length);
        //  Bitmap image = BitmapFactory.decodeStream(imagestream);
            img = (ImageView) findViewById (R.id.gambar);
            img.setImageBitmap(imagestream); 
        hasilCari.setText("Hasil Pencarian : " + cursor.getInt(0));
    } catch (SQLException e) {
        hasilCari.setText(e.toString());
    }
}

我只是不知道如何解决这个问题。当我尝试在我的应用程序中查找另一个字母时,图像将始终出现在数据库的第 1 行中。请帮我 :)

*对不起,我的英语不好

4

1 回答 1

0

哦,伙计,我很抱歉,但我必须说:你的代码真的很糟糕......

它根本没有组织,但也许......不应该byte[] im = cur.getBlob(3);byte[] im = cursor.getBlob(3);


编辑:尝试评论第二个和第三个查询,因为它们看起来没用,并进行我建议的“cur”->“cursor”更改:

    public void findAlphabet(View view) {
        try {
            db = kamusTI.getWritableDatabase();
            String sql = "SELECT * FROM kamusti WHERE istilah LIKE '"
                    + editAlphabet.getText().toString() + "%' ORDER BY istilah";
            cursor = db.rawQuery(sql, null);            
            adapter2 = new SimpleCursorAdapter(this,
                    android.R.layout.simple_list_item_2, cursor, new String[] {
                            "istilah", "definisi" }, new int[] {
                            android.R.id.text1, android.R.id.text2 });
            listView2.setAdapter(adapter2);
            listView2.setTextFilterEnabled(true);
//            String sql1 = "SELECT COUNT(istilah) FROM kamusti WHERE istilah LIKE '"
//                    + editAlphabet.getText().toString() + "%' ORDER BY istilah";
//            cursor = db.rawQuery(sql1, null);
              cursor.moveToFirst();

//            cur = db.query("kamusti", null, null, null, null, null, null);
//            cur.moveToFirst();
        //  cur.moveToPosition(0);
            byte[] im = cursor.getBlob(3);
            //  ByteArrayInputStream imagestream = new ByteArrayInputStream(im);
                Bitmap imagestream = BitmapFactory.decodeByteArray(im, 0, im.length);
            //  Bitmap image = BitmapFactory.decodeStream(imagestream);
                img = (ImageView) findViewById (R.id.gambar);
                img.setImageBitmap(imagestream); 
            hasilCari.setText("Hasil Pencarian : " + cursor.getInt(0));
        } catch (SQLException e) {
            hasilCari.setText(e.toString());
        }
    }
于 2013-01-12T13:00:57.003 回答