Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将SimpleCursorAdapter与图像一起使用(路径存储在SQLite中)_Java_Android_Sqlite_Listview - Fatal编程技术网

Java 将SimpleCursorAdapter与图像一起使用(路径存储在SQLite中)

Java 将SimpleCursorAdapter与图像一起使用(路径存储在SQLite中),java,android,sqlite,listview,Java,Android,Sqlite,Listview,我有一个现有的ListView,其中包含来自sqlite数据库的一些信息。 用户可以创建狗的配置文件。 创建一些配置文件后,列表如下所示: 抱歉的链接,但我需要10个代表直接图片 正如你所看到的,有一个地方可以放狗的图片 在创建个人资料时,用户可以添加狗的图片,该图片保存在内部存储器/Android/data/Files/*.jpg中。我不想将其添加到sqlite数据库中。 它的安全名称等于SQLite数据库中表行的id 假设我添加了一条新狗,行的id是134,图片的名称是134.jpg 但是

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

抱歉的链接,但我需要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);

}
如果你需要更多的信息,请询问,但我认为我需要的是或多或少的提示/想法,如何将图片添加到listview中

编辑:因为梅勒韦德的答案

谢谢@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);
但它还是不给我看照片

可以使用对象将ImageView绑定到光标

请看一个好例子。我在复制这个例子,如果链接断了的话

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

我有一个相同的问题,尽管我在日志中发现ViewBinder只签入到,R.id.imageView1不签入到,但[ifview.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 };

谢谢你的快速回答。我仍然有问题。我编辑了第一篇文章,因为我不能在8小时内回答我自己的帖子,只有10个代表:我没有得到它。它没有显示任何东西?还是只显示图片?其余显示如第一个屏幕所示。只是照片还没出现