0

我使用 simplecursoradapter 填充了一个列表视图。但是,我想添加图像,其中如果答案正确,则应在右侧显示检查,如果为空或不正确,则应显示 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;

}
}
}
4

1 回答 1

0

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

您应该覆盖newView()and bindView()

newView()膨胀您的布局并初始化您的视图。

bindView()设置您的视图值。

于 2012-09-14T09:15:15.967 回答