无法使用简单的光标适配器在listview上显示图像

时间:2012-09-14 09:05:27

标签: android sqlite simplecursoradapter custom-lists

我使用simplecursoradapter填充了listview。但是,我想添加图像,其中如果答案是正确的,则应在右侧显示检查,如果为空或不正确,则应显示x标记。它没有显示任何内容,但没有错误。这是我的活动代码:

public class Results extends ListActivity{
DBAdapter db = new DBAdapter(this);
private Cursor mCursor;
ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.resultslist);

    db.open();
    fillData();
    db.close();

}
private void fillData() {

    mCursor = db.getAllInfo();
    startManagingCursor(mCursor);
    String[] from = new String[]{DBAdapter.KEY_QUESTIONS, DBAdapter.KEY_CORRECTANSWERS, DBAdapter.KEY_YOURANSWERS}; 
    int[] to = new int[]{R.id.textViewquestionresults, R.id.textViewcorrectansresults, R.id.textViewyouranswerresults};

    SimpleCursorAdapter c=
            new SimpleCursorAdapter(this, R.layout.rowresults, mCursor, from, to);


        setListAdapter(c);

}
private class c extends SimpleCursorAdapter{

        Context lcontext;


    public c(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        lcontext = context;

    }
    @Override
    public View getView(final int pos, View v, ViewGroup parent) {
    v = super.getView(pos, v, parent);
    final ImageView iv = (ImageView) v.findViewById(R.id.imageViewresults);
    final TextView tvQuestion = (TextView) v.findViewById(R.id.textViewQuestion);
    final TextView tvCorrectAns = (TextView) v.findViewById(R.id.textViewcorrectansresults);
    final TextView tvYourAns = (TextView) v.findViewById(R.id.textViewyouranswerresults);
     if(tvYourAns.equals(tvCorrectAns)){
        iv.setImageResource(R.drawable.greencheckmark);
    }else{
        iv.setImageResource(R.drawable.redxmark);
    }
    return v;

}
}
}

1 个答案:

答案 0 :(得分:0)

扩展SimpleCursorAdapter时,不应覆盖getView()

您应该覆盖newView()bindView()

newView()中夸大您的布局并初始化您的观看次数。

bindView()中设置您的观看次数值。