将SimpleCursorAdapter与Image一起使用(存储在SQLite中的路径)

时间:2014-03-10 20:29:45

标签: java android sqlite listview

我有一个现有的ListView,它填充了sqlite数据库中的一些信息。 用户可以创建狗的配置文件。 创建一些配置文件后,列表看起来像:

Example (对不起链接,但我需要10个代表直接拍照)

如你所见,有照片的地方(狗图像)。

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

假设我添加一条新狗,该行的ID为134,图片名称为“134.jpg”

好吧但是我不知道如何将这张图片添加到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);

}

如果您需要更多信息,请询问,但我认为我需要的重要事项或多或少是暗示/想法如何将这些动作添加到列表视图中

编辑 (因为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);

但它仍然没有给我看图片。

2 个答案:

答案 0 :(得分:0)

您可以使用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;
   }
}); 

答案 1 :(得分:0)

我有一个同样的问题,尽管Log()我发现ViewBinder仅在“至”中检查项目,而R.id.imageView1在“至”中却没有,[if(view.getId()==R。 id.imageView1)]将不会进入。
您必须在“至”中添加R.id.imageView1,并在“自”中添加一个字符串(甚至重复)。

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