0

SQLite データベースからの既存の ListView があります。ユーザーがギャラリーからリストビューに写真を追加できるようにしたい。これらの写真は内部ストレージに「*.jpg」として保存されます。* は、新しいアカウントが作成された場合の特定の行 ID です。

例: SQLite データベースの新しいエントリがテーブル (id) 番号 8 で作成されます。次に、ユーザーが写真を選択した場合、これは「8.jpg」という名前で内部ストレージに保存されます。

私の問題は、その写真をlistViewの正確な位置に表示する方法です...これまでの私のコードは次のとおりです。

編集:(更新されたコード)

private void fillData() {

    mNotesCursor = helper.fetchAllData();

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

    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,
            R.id.imageButton1};


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

    mMyListView.setAdapter(adapter);

    adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){

           public boolean setViewValue(View view, Cursor cursor, int columnIndex){

               final int id = mNotesCursor.getInt(mNotesCursor.getColumnIndex(MySQLiteHelper.UID));
               LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
               view = inflater.inflate(R.layout.hundeliste_item, null);

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

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

               Log.v("TEST COMPARISON", "columnIndex=" + columnIndex + "  ID = " + id + "  URI = " + uri); 


// I think here comes my mistake, but I don't know another solution                

if(columnIndex == cursor.getColumnIndex(MySQLiteHelper.PHOTO)) {


                      ((ImageView)view.findViewById(R.id.imageView1)).setImageDrawable(Drawable.createFromPath(uri.toString()));
                      Log.v("Test", "... this Log don't show up, cause columnIndex =/= id");  

                      return true;

               }



                       return false; 
                 }
                }); 

どんな助けでも大歓迎です。これに夢中になる..解決策なしでここに1週間座っている

編集: (LogOutput)

03-11 12:55:36.784: V/TEST COMPARISON(22456): columnIndex=1  ID = 1  URI = /storage/emulated/0/Android/data/com.example.android.navigationdrawerexample/Files/1.jpg
    03-11 12:55:36.794: V/TEST COMPARISON(22456): columnIndex=2  ID = 1  URI = /storage/emulated/0/Android/data/com.example.android.navigationdrawerexample/Files/1.jpg
    03-11 12:55:36.804: V/TEST COMPARISON(22456): columnIndex=7  ID = 1  URI = /storage/emulated/0/Android/data/com.example.android.navigationdrawerexample/Files/1.jpg
    03-11 12:55:36.814: V/TEST COMPARISON(22456): columnIndex=1  ID = 2  URI = /storage/emulated/0/Android/data/com.example.android.navigationdrawerexample/Files/2.jpg
    03-11 12:55:36.824: V/TEST COMPARISON(22456): columnIndex=2  ID = 2  URI = /storage/emulated/0/Android/data/com.example.android.navigationdrawerexample/Files/2.jpg
    03-11 12:55:36.834: V/TEST COMPARISON(22456): columnIndex=7  ID = 2  URI = /storage/emulated/0/Android/data/com.example.android.navigationdrawerexample/Files/2.jpg
    03-11 12:55:36.844: V/TEST COMPARISON(22456): columnIndex=1  ID = 3  URI = /storage/emulated/0/Android/data/com.example.android.navigationdrawerexample/Files/3.jpg
    03-11 12:55:36.844: V/TEST COMPARISON(22456): columnIndex=2  ID = 3  URI = /storage/emulated/0/Android/data/com.example.android.navigationdrawerexample/Files/3.jpg
    03-11 12:55:36.854: V/TEST COMPARISON(22456): columnIndex=7  ID = 3  URI = /storage/emulated/0/Android/data/com.example.android.navigationdrawerexample/Files/3.jpg
4

1 に答える 1

0

わかりましたので、あなたの場合はオーバーライドする方が良いgetViewと思いsetViewValueます。カーソル内にある列に対して呼び出されますが、イメージはそうではありません(この方法では、すべての列でイメージをオーバーライドし、パフォーマンスが低下します)。

でビューを作成する方法の例を次に示しますgetView

EDITbindViewをandに変更newView

private void fillData() {

    mNotesCursor = helper.fetchAllData();

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

    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,
        R.id.imageButton1};


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

    mMyListView.setAdapter(adapter);

}


 private class MyAdapter extends SimpleCursorAdapter{

        private Context mContext;
        private int layout;
        private Cursor cursor;
        private final LayoutInflater inflater;

        public MyAdapter(Context context,int layout, Cursor cursor, String[] from, int[] to) {
            super(context,layout,cursor,from,to);
            this.layout=layout;
            this.mContext = context;
            this.inflater=LayoutInflater.from(context);
            this.cursor=cursor;
        }

        @Override
        public View newView (Context context, Cursor cursor, ViewGroup parent) {
                return inflater.inflate(layout, null);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            super.bindView(view, context, cursor);

            // Set textviews analogue to this
            TextView name = (TextView) view.findViewById(R.id.label);
            name.setText(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.NAME)));

            // Set the values for other data elements
            // ....

            // Create the imageView
            final int id = cursor.getInt(cursor.getColumnIndex(MySQLiteHelper.UID));
            ImageView imageView = (ImageView) view.findViewById(R.id.imageButton1);

            // load the image from the storage location
            // (a check if the image exists would be nice)
            String mediaStorageFilePath = Environment.getExternalStorageDirectory()
                    + File.separator +"Android"
                    + File.separator +"data"
                    + File.separator + getActivity().getPackageName()
                    + File.separator + "Files"
                    + File.separator + id +".jpg";

            // load the image as a bitmap and set it to the image view
            Bitmap bmp = BitmapFactory.decodeFile(mediaStorageFilePath);
            imageView.setImageBitmap(bmp);

        }
    }
}

これが役立つことを願っています。

于 2014-03-11T12:48:17.307 に答える