【问题标题】:Use SimpleCursorAdapter with Image (path stored in SQLite)将 SimpleCursorAdapter 与 Image 一起使用(存储在 SQLite 中的路径)
【发布时间】:2014-03-10 20:29:44
【问题描述】:

我有一个现有的 ListView,其中填充了来自 sqlite 数据库的一些信息。 用户可以创建狗的档案。 创建一些配置文件后,列表如下所示:

Example (抱歉链接,但我需要 10 Rep 才能直接拍照)

如您所见,这里有放置图片(狗图片)的地方。

在创建配置文件时,用户可以从他的狗中添加一张图片,该图片保存在内部存储 /Android/data/Files/*.jpg 中(我不想将其添加到 sqlite 数据库中)。 用于保护它的名称等于 SQLite 数据库中表行的 id。

假设我添加了一条新狗,行的 id 是 134,图片的名称是“134.jpg”

嗯,但是 atm 我不知道如何将这张图片添加到 ListView 中。

欢迎任何帮助!

最后但并非最不重要的是我如何从 sqlite 获取数据的代码和平:

private void fillData() {

    mNotesCursor = helper.fetchAllData();


    String[] from = new String[] { MyDatabaseAdapter.MySQLiteHelper.NAME,
            MyDatabaseAdapter.MySQLiteHelper.PASSWORD,
            MySQLiteHelper.CB_GETREIDE, MySQLiteHelper.CB_FASTENTAG,
            MySQLiteHelper.CB_WOCHENPLAN, MySQLiteHelper.CB_DIET,
            MySQLiteHelper.SP_ART  };


    // Fields on the UI to which we map
    int[] to = new int[] { R.id.label, R.id.gewicht, 
            R.id.getreide,
            R.id.fastentag, 
            R.id.wochenplan, 
            R.id.diet, 
            R.id.spinner
            };

    adapter = new SimpleCursorAdapter(getActivity(),
            R.layout.hundeliste_item, mNotesCursor, from, to, 0);

    mMyListView.setAdapter(adapter);

}

如果您需要更多信息,请询问,但我认为我需要或多或少的提示/想法如何将 pivture 添加到列表视图中

编辑: (因为 Merlevede 的回答)

感谢@Merlevede

我尝试了以下方法:

//... see post #1 for the upper code part

adapter = new SimpleCursorAdapter(getActivity(),
                R.layout.hundeliste_item, mNotesCursor, from, to, 0);



        adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
           // Binds the Cursor column defined by the specified index to the specified view 
           public boolean setViewValue(View view, Cursor cursor, int columnIndex){

               LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
               view = inflater.inflate(R.layout.hundeliste_item, null);

                   if(view.getId() == R.id.imageView1){

                   File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
                            + "/Android/data/"
                            + getActivity().getPackageName()
                            + "/Files"); 

                   Uri uri = Uri.parse(mediaStorageDir.getPath() + File.separator + item_id +".jpg");


                   ((ImageView)view.findViewById(R.id.imageView1)).setImageDrawable(Drawable.createFromPath(uri.toString()));
return true; //true because the data was bound to the view
                   } 
                   return false; 
             }
            });
    mMyListView.setAdapter(adapter);

但它仍然没有向我显示图片。

【问题讨论】:

  • 我没听懂。它没有显示任何东西?还是只有图片?
  • 其余的显示在第一个屏幕中。只是图片仍然没有显示

标签: java android sqlite listview


【解决方案1】:

您可以使用SimpleCursorAdapter.ViewBinder 对象将 ImageView 绑定到光标。

看看this link 就是一个很好的例子。如果链接损坏,我正在复制示例。

listAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
   /** Binds the Cursor column defined by the specified index to the specified view */
   public boolean setViewValue(View view, Cursor cursor, int columnIndex){
       if(view.getId() == R.id.your_image_view_id){
           //...
           ((ImageView)view).setImageDrawable(...);
           return true; //true because the data was bound to the view
       }
       return false;
   }
}); 

【讨论】:

  • 感谢您的快速答复。我还有问题。我编辑了第一篇文章,因为我无法在 8 小时内回答我自己的帖子,只有 10 个 Rep :(
【解决方案2】:

我有同样的问题,虽然 Log() 我发现 ViewBinder 只在“to”中检查项目,R.id.imageView1 不在“to”中,[ if(view.getId() == R. id.imageView1) ] 不会进入。
您必须在“to”中添加 R.id.imageView1,并且还要在“from”中添加一个字符串(甚至重复)。

String[] from = { MyDatabaseAdapter.MySQLiteHelper.NAME,
                  MyDatabaseAdapter.MySQLiteHelper.PASSWORD,
                  MySQLiteHelper.CB_GETREIDE,
                  MySQLiteHelper.CB_FASTENTAG,
                  MySQLiteHelper.CB_WOCHENPLAN,
                  MySQLiteHelper.CB_DIET,
                  MySQLiteHelper.SP_ART,
                  MySQLiteHelper.SP_ART };
int[] to = { R.id.label,
             R.id.gewicht, 
             R.id.getreide,
             R.id.fastentag, 
             R.id.wochenplan, 
             R.id.diet, 
             R.id.spinner,
             R.id.imageView1 };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-24
    • 2021-07-18
    • 2011-08-07
    • 1970-01-01
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多