在使用 SimpleCursorAdapter 显示到列表视图之前检查记录的内容

Checking the contents of the record before it is shown to listview using SimpleCursorAdapter

如果第 "status"==0 列,我想显示一个灰色的星星图像,如果第 "status"==1 列,我想显示一个金色的星星图像。我的代码可以将记录显示到 listview,但没有先检查 "status" 列:

public void loadPertanyaan(){
        Cursor soal = DBAdapter.fetchSemuaSoal();
        String[] from = new String[]{DBAdapter.KEY_SOAL_PERTANYAAN};
        int[] to = new int[]{R.id.txt_PilihSoal_Pertanyaan};
        SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(this, R.layout.list_row, soal, from, to);
        setListAdapter(dataAdapter);
    }

然后我添加代码以在 setListAdapter(dataAdapter) 之后使用 moveToNext() 检查 status 列。但是应用程序总是强制停止,代码检查下面的 "answered" 列 -

    ImageView img = (ImageView) findViewById(R.id.imgBintang);
    while(soal.moveToNext()){
        if(soal.getInt(soal.getColumnIndex("status"))==1)
            img.setImageResource(R.drawable.bintang_mas);
        else
            img.setImageResource(R.drawable.bintang_abu);
    }

如何在显示到 ListView 之前检查 "status" 列。提前致谢。

Agi,你需要像 link Populating a ListView with a CursorAdapter 这样的好教程。查看代码 bindView 类似于 getView 方法,在之前的回答中提到过。 getView 的强度较低,因此它可能对您来说已经足够了。

查看 LinearLayout 资源 XML。其中,有 2 个 TextView 元素。相反,我认为您的 ListView 中需要更多列,例如 TextView、流行的 Checkbox 和星号 (bintang) 的图像。

代码和你设计的想法不一样。它以虚拟方式获取并显示数据,您可以通过positionCursor参数查看数据。

祝你好运,告诉我你的情况如何...