Android的SimpleCursorAdapter - 添加条件图片 [英] Android SimpleCursorAdapter - Adding conditional images

查看:138
本文介绍了Android的SimpleCursorAdapter - 添加条件图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我用SimpleCursorAdapter从SQLite的数据适配到ListView控件。让我们把这个数据库TESTDATA。我的一位在TESTDATA列的记录真或假0或1。我可以让列表视图显示不同图像的每个项目,根据该行是否有0或1?

这是我使用的适配器。

  ListAdapter适配器=新SimpleCursorAdapter(
本,
android.R.layout.two_line_list_item,
mCursor,
新的String [] {testData.DATE1,testData.NAME1},
新的INT [] {android.R.id.text1,android.R.id.text2});
 

解决方案

我创建了一个自定义的SimpleCursorAdapter:

 公共类MySimpleCursorAdapter扩展SimpleCursorAdapter {

    公共MySimpleCursorAdapter(上下文的背景下,INT布局,光标CUR,
             的String []从,INT []键){
        超(背景下,布局,CUR,从,到);
    }

    @覆盖公共无效setViewImage(ImageView的四,字符串文本)
    {
        如果(text.equals(0)){
            iv.setImageResource(R.drawable.new1);
        }
        其他 {
            iv.setImageResource(R.drawable.check1);
        }
    }

}
 

在我ListActivity我绑定在数据库中的图像资源ID文本字段。在你的榜样,它是这样的:

  ListAdapter适配器=新MySimpleCursorAdapter(
    本,
    android.R.layout.two_line_list_item,
    mCursor,
    新的String [] {testData.DATE1,testData.NAME1},
    新的INT [] {android.R.id.text1,android.R.id.image1}); //注:与此搜索替换文本2
 

即。如果在数据库中的文本字段名称1包含0,图像名new1将显示在ListView,并且如果它具有另一个值,为check1将被显示。

So I'm using SimpleCursorAdapter to adapt data from SQLite into ListView. Lets call this database testData. One of my columns in testData records true or false with either 0 or 1. Can I make the listview display a different image for each item according to whether that row has 0 or 1?

This is the adapter that I'm using.

ListAdapter adapter = new SimpleCursorAdapter(
this, 
android.R.layout.two_line_list_item,  
mCursor,                                              
new String[] {testData.DATE1, testData.NAME1},           
new int[] {android.R.id.text1, android.R.id.text2}); 

解决方案

I created a customized SimpleCursorAdapter:

public class MySimpleCursorAdapter extends SimpleCursorAdapter {

    public MySimpleCursorAdapter(Context context, int layout, Cursor cur,
             String[] from, int[] to) {
        super(context, layout, cur, from, to);
    }

    @Override public void setViewImage(ImageView iv, String text)
    {
        if (text.equals("0")) {
            iv.setImageResource(R.drawable.new1);
        }
        else {
            iv.setImageResource(R.drawable.check1);
        }
    }

}

In my ListActivity I bind the text field in the database to the image resource id. In your example it would look like this:

ListAdapter adapter = new MySimpleCursorAdapter(
    this, 
    android.R.layout.two_line_list_item,  
    mCursor,                                              
    new String[] {testData.DATE1, testData.NAME1},           
    new int[] {android.R.id.text1, android.R.id.image1}); // Note: replace text2 with image1

I.e. if the text field "Name1" in your database contains "0", the image "new1" will be displayed in the ListView, and if it has another value, "check1" will be displayed.

这篇关于Android的SimpleCursorAdapter - 添加条件图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆