从SQLite数据库ListView中显示的图像 [英] Displaying an image in ListView from SQLite database

查看:195
本文介绍了从SQLite数据库ListView中显示的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点停留在那一刻。我知道类似的问题已经被问过,但我一直没能找到任何帮助。

我试图让和图像在ListView控件从我的Andr​​oid应用程序内的内部数据库中出现。我有141行的数据库,每一行标1,2或3。我需要一个不同的PNG图像(保存在我的RES /绘制的文件夹),取决于1,2或3。这是我目前的显示查询。任何意见将受到欢迎。我意识到有可能会显示我需要的信息更好的办法。

 公共无效whosnext(查看视图){
    // ||在SQLite的串联操作
    光标= db.rawQuery(SELECT * FROM eventsv1 WHERE START_TIME>(DATETIME('现在'))和标题LIKE ORDER BY ASC日期,时间DESC
                    新的String [] {%+ searchText.getText()的toString()+%});
    适配器=新SimpleCursorAdapter(
            这个,
            R.layout.artist_list_item,
            光标,
            新的String [] {称号,时间,日期,TITLE3,风格},
            新的INT [] {R.id.title,R.id.time,R.id.date,R.id.title3,R.id.style});
    setListAdapter(适配器);
}


解决方案

我会写的是定制的CursorAdapter

 公共类WhosNextAdapter扩展的CursorAdapter
{
    公共WhosNextAdapter(上下文的背景下,光标光标,布尔autoRequery){
        超(上下文,光标,autoRequery);
    }    @覆盖
    公共无效bindView(查看视图,上下文的背景下,光标光标){
        //这是被你会检查你的光标样式(我认为这是你的1,2,3列)
        INT风格= cursor.getInt(cursor.getColumnIndex(风格));        ImageView的IMG =(ImageView的)view.findViewById(R.id.yourImageViewId);        开关(风格){
            情况1:
                img.setImageResource(R.drawable.yourImageForStyle1);            打破;
            案例2:
                img.setImageResource(R.drawable.yourImageForStyle2);            打破;//等等。
        }
    }    @覆盖
    公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
        //充气布局R.layout.artist_list_item        //调用bindView通过虚增布局,环境,和光标        //回路布局
    }
}

I'm a little stuck at the moment. I know similar questions have been asked, but I haven't been able to find anything that has helped.

I'm trying to get and image to appear in ListView from an internal database inside my android application. I have a database with 141 rows, and each row is labeled 1,2 or 3. I need a different png image (saved in my res/drawable folder) to show depending on the 1,2, or 3. Here is my current Query. Any advice would be welcome. I realize there may be a better way to display the info I need.

   public void whosnext(View view) {
    // || is the concatenation operation in SQLite
    cursor = db.rawQuery("SELECT * FROM eventsv1 WHERE start_time > (DATETIME('now')) AND title LIKE ? ORDER BY date ASC, time DESC", 
                    new String[]{"%" + searchText.getText().toString() + "%"});
    adapter = new SimpleCursorAdapter(
            this, 
            R.layout.artist_list_item, 
            cursor, 
            new String[] {"title", "time", "date", "title3", "style"}, 
            new int[] {R.id.title, R.id.time, R.id.date, R.id.title3, R.id.style});
    setListAdapter(adapter);
}

解决方案

I would write a custome CursorAdapter

public class WhosNextAdapter extends CursorAdapter
{
    public WhosNextAdapter(Context context, Cursor cursor, boolean autoRequery) {
        super(context, cursor, autoRequery);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        //This is were you would check your cursor for style (I think that is 1,2,3 your column)
        int style = cursor.getInt(cursor.getColumnIndex("style"));

        ImageView img = (ImageView) view.findViewById(R.id.yourImageViewId);

        switch (style) {
            case 1:
                img.setImageResource(R.drawable.yourImageForStyle1);

            break;
            case 2:
                img.setImageResource(R.drawable.yourImageForStyle2);

            break;

//etc.
        }
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        //Inflate layout R.layout.artist_list_item

        //Call bindView passing inflated layout, context, and cursor

        //return layout
    }
}

这篇关于从SQLite数据库ListView中显示的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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