0

次のようにデータベースをListViewに接続します。

public void update_list(String NN) {
       c = db.rawQuery(NN, null);
       startManagingCursor(c);
       String[] from = new String[]{"_id","Fname","Lname","Phone","Car","CarNum" };
       int[] to = new int[]{  R.id._id,R.id.Fname,R.id.Lname,R.id.Phone,R.id.Car,R.id.CarNum };
       SimpleCursorAdapter notes = new SimpleCursorAdapter (this, R.layout.my_list, c, from, to);
       setListAdapter(notes);
       setListAdapter(new SimpleCursorAdapter(this, R.layout.my_list, c, from,to) {

       @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = super.getView(position, convertView, parent);
        }
    });
    }

今、私はImageViewを画像に接続する必要があります

写真へのリンクを保持するフィールドPicNumがあります

私はこのように画像をImageViewにロードすることを知っています:

 MyPic = (ImageView) findViewById(R.id.MyPic);
        try
        {
        File imgFile = new File("/sdcard/MyPic/Unzip/" +MyParam.zPicNum+ ".jpeg");
        if(imgFile.exists()){
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
             MyPic.setImageBitmap(myBitmap);
            }
        else
        {
             MyPic.setImageBitmap(null);
        }
      }
      catch (Exception e) {
           MyPic.setImageBitmap(null);
    } 

このImageViewを私のListViewに組み合わせる方法は?

4

5 に答える 5

3

リストビューに画像を挿入するには、次のようにしてください。

MySimpleAdapter notes;

    public class MySimpleAdapter extends SimpleCursorAdapter{

            public MySimpleAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
                super(context, layout, c, from, to);
            }

            @Override
            public void setViewImage(ImageView v, String zPicNum) {
                try{
                    String pathName = Environment.getExternalStorageDirectory().getPath() + "MyPic/Unzip/" +zPicNum+ ".jpeg";
                    File path = new File(pathName);
                    if(path.exists()){
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        Bitmap bm = BitmapFactory.decodeFile(pathName, options);
                        v.setImageBitmap(bm);
                    }
                    else{
                        v.setImageResource(R.drawable.defaultpic);
                    }
                }
                catch (Exception e)
                {
                   Toast.makeText(getActivity(), "error in finding images", Toast.LENGTH_LONG).show();
                }
            }
        }
于 2012-11-21T22:39:59.550 に答える
2

&配列にPicNumフィールドとR.id.MyPic(の一部である必要がありR.layout.my_listます)を追加します。fromto

   String[] from = new String[]{"_id","Fname","Lname","Phone","Car","CarNum","PicNum" };
   int[] to = new int[]{R.id._id,R.id.Fname,R.id.Lname,R.id.Phone,R.id.Car,R.id.CarNum, R.id.MyPic};

次に、メソッドを使用して、画像を返す前にgetView()画像を塗りつぶします。R.id.MyPic

@Override
 public View getView(int position, View convertView, ViewGroup parent) {
  if(convertView.getId()==R.id.MyPic){
    /*Your code to load the Picture to convertView*/
    return convertView;
    }else return super.getView(position, convertView, parent);
   }
 });

もう1つのオプションは、を使用してバインドされているViewBinderかどうかを確認してから、画像を再度ロードすることです。ViewR.id.MyPic

于 2012-11-21T21:56:45.470 に答える
2

SimpleCursorAdapter理由により非推奨になりました。その理由は、現在のビューポートに表示されているすべての画像をダウンロードするまで、アプリケーション全体がフリーズするためです。

于 2012-11-21T22:08:01.307 に答える
1

PicNumには何が含まれていますか?画像へのURLの場合は、表示する前にファイルをダウンロードしてデコードする必要があるため、SimpleCursorAdapterを単独で使用することはできません。必要に応じて、SimpleCursorAdapterをサブクラス化し、SimpleCursorAdapter.ViewBinderインターフェースを使用して拡張することもできます。これにより、ほとんどの列でデフォルトのbindView動作を使用できますが、PicNumのイメージをダウンロードしてデコードします。

于 2012-11-21T21:57:54.940 に答える
1

SimpleCursor Adapterは、実装が非常に簡単です。これは、SimpleCursorAdapterの学習に役立つものです。ここをクリックしてください。

于 2012-11-22T00:26:44.617 に答える