简体   繁体   English

从SQLite数据库在ListView中显示图像

[英]Displaying an image in ListView from SQLite database

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. 我正在尝试从Android应用程序内部的内部数据库中获取图像并使其出现在ListView中。 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. 我有一个数据库,其中包含141行,每行分别标记为1,2或3。我需要一个不同的png图像(保存在我的res / drawable文件夹中)才能显示,具体取决于1,2或3。这是我当前的查询。 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 我会写一个自定义的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
    }
}

You definitely need to extend SimpleCursorAdapter and if your item layout is simple enough, the only thing you need to do is to override setViewImage() method where you simply convert provided value from the database and call setImageResource() method on the ImageView object. 您绝对需要扩展SimpleCursorAdapter并且如果您的项目布局足够简单,那么您唯一需要做的就是重写setViewImage()方法,在该方法中,您只需从数据库转换提供的值,然后对ImageView对象调用setImageResource()方法。 Then you won't have to alternate much your attached code. 这样,您就不必替换大量的附加代码。

public class MySimpleCursorAdpater extends SimpleCursorAdapter {

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

    @Override
    public void setViewImage(ImageView v, String value) {
        /*your code to convert value from database to drawable resource id*/
        v.setImageResource(resourceId);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM