2012-11-21 2 views
0

에 이미지 뷰를 결합하는 방법이 같은 ListView에 내 데이터베이스를 연결합니다은 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); 
     } 
    }); 
    } 

지금 내가

내가이 링크를 길게 필드 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); 
    } 

이 Ima를 결합하는 방법 내 ListView에 geView?

답변

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(); 
       } 
      } 
     } 
+0

도움을 주셔서 감사합니다.하지만 이걸 시도해 볼 수는 없지만, 내 사진을 볼 수는 없습니다. 더 많은 도움을받을 수 있습니까? – Gold

+0

OK, 제 문제를 고쳤습니다. – Gold

2

PicNum 필드를 추가하고 R.id.MyPicfrom & to 배열에서 (이 R.layout.my_list의 일부가되어야합니다) :

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

그런 다음 그것을 반환하기 전에 R.id.MyPic의 그림을 채우기 위해 getView() 방법을 사용하십시오

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

또 다른 옵션은 ViewBinder을 사용하고 Viewb 바인딩이 R.id.MyPic이면 다시 그림을로드하십시오.

+0

감사를 배울 도움이 될 것입니다 하나입니다 도와 줘요. 이걸 시도해 봐요.하지만 내 그림과 프로그램을 볼 수 없어요. – Gold

1

PicNum에는 어떤 것이 있습니까? 이미지의 URL 인 경우 이미지를 표시하기 전에 파일을 다운로드하여 디코딩해야하므로 SimpleCursorAdapter를 단독으로 사용할 수 없습니다. 원하는 경우 SimpleCursorAdapter를 하위 클래스로 만들고 SimpleCursorAdapter.ViewBinder 인터페이스로 확장 할 수도 있습니다. 그러면 대부분의 열에 대해 기본 bindView 비헤이비어를 가질 수 있지만 PicNum의 이미지를 다운로드하고 디코딩 할 수 있습니다.

2

SimpleCursorAdapter은 이유가 없어졌습니다. 그 이유는 현재 뷰포트에 표시되는 모든 이미지를 다운로드 할 때까지 전체 응용 프로그램을 고정시킵니다.

1

SimpleCursor 어댑터를 구현하는 것은 매우 쉽습니다, 다음은 간단한 커서 어댑터에게 대한 Click Here