【问题标题】:Displaying an image in ListView from SQLite database从 SQLite 数据库在 ListView 中显示图像
【发布时间】:2011-06-27 16:51:28
【问题描述】:

我现在有点卡住了。我知道有人问过类似的问题,但我找不到任何有帮助的东西。

我正在尝试从我的 android 应用程序的内部数据库中获取并显示在 ListView 中的图像。我有一个包含 141 行的数据库,每行标记为 1,2 或 3。我需要不同的 png 图像(保存在我的 res/drawable 文件夹中)来显示取决于 1,2 或 3。这是我当前的询问。任何的建议都受欢迎。我意识到可能有更好的方式来显示我需要的信息。

   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);
}

【问题讨论】:

    标签: android sqlite android-listview android-imageview


    【解决方案1】:

    我会写一个自定义 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
        }
    }
    

    【讨论】:

    • 我添加了新的 WhosNextAdapter,现在在我的列表视图中,文本已调整为环绕图像应该在的位置,但我的图像没有显示。我需要在我的主要活动中打一个电话,还是我需要告诉系统它是一个 png 或从哪个文件夹中绘制图像?感谢您迄今为止的帮助。
    【解决方案2】:

    您肯定需要扩展SimpleCursorAdapter,如果您的项目布局足够简单,您唯一需要做的就是覆盖setViewImage() 方法,您只需从数据库中转换提供的值并调用setImageResource() 方法ImageView 对象。这样您就不必更改很多附加代码。

    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);
        }
    
    }
    

    【讨论】:

    • 我仍然有问题。如何将 ImageView 调用到我在最初发布的代码中使用的 SimpleCusorAdapter 中。这是一个 gdoc 链接,其中包含我的完整主要活动代码。 "_https://docs.google.com/document/d/140t6QKBjT14c4mEcRitCw4Wb8FbVg3FF6DxgGot8RKM/edit?hl=en_US&authkey=CI-W29MC"
    • 我不确定这是否有帮助,这可能是我问题的根源。这是我用来访问我的 SQLite DB 的 DatabaseHelper。 "_https://docs.google.com/document/d/1LWmx1vLI2NljDNjVq5nwNpdnFAieY1aHfkiuWf-RqL0/edit?hl=en_US&authkey=CIWV08wG"
    • 你能修复一个指向你的活动代码的链接吗?你还确定你的数据库表有名为_id 的列被适配器使用吗?你有什么样的问题?你能发布你的 logcat 输出吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多