Search code examples
androidlistviewsimplecursoradapter

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


I would like to display a gray star image if column "status"==0 and display a golden star if column "status"==1. My code can display the record to listview, but without checking the "status" column first:

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);
    }

Then I add code to check status column using moveToNext() after setListAdapter(dataAdapter). But the application always force stopped, code to check "answered" column below -

    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);
    }

How can I check "status" column before displaying to ListView. Thanks in advance.


Solution

  • Agi, You need a good tutorial like link Populating a ListView with a CursorAdapter . Look at code bindView similar to getView method, mentioned in a previous answer. getView is less intensive so it may be good enough for you.

    Look at LinearLayout resource XML. In it, there are 2 TextView elements. Instead, I think you need more columns in your ListView, like TextView, popular Checkbox, and an image for the star (bintang).

    The code is different than your thoughts in design. It gets and displays data in virtual mode, and you can check the data by the position or Cursor parameter.

    Good luck and tell me how it goes for you...