【问题标题】:Cannot display image on listview using simple cursor adapter无法使用简单的光标适配器在列表视图上显示图像
【发布时间】:2012-09-14 09:05:26
【问题描述】:

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

}
}
}

【问题讨论】:

    标签: android sqlite simplecursoradapter custom-lists


    【解决方案1】:

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

    您应该改写newView()bindView()

    newView() 中扩充您的布局并初始化您的视图。

    bindView() 中设置您的视图值。

    【讨论】:

    • 感谢您的回复。我明天会尝试,因为我不知道如何使用这些方法......我明天会做更多的研究。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    相关资源
    最近更新 更多