2012-12-24 1 views
0

안녕하세요. 검색 한 결과 최선을 다했지만 작동하도록 만들지 못했습니다. 목록보기가있어 이미지가있는 휴대 전화 연락처로로드됩니다. 아래에있는 내 코드는Android : Listview 문제 - 이미지가 잘못 할당되었습니다.

public ProfileAdapter(Activity activity, int textViewResourceId, List<Profile> listCont,int renderer, String profileType) { 
    super(activity, textViewResourceId, listCont); 
    this.renderer = renderer; 
    this.listCont = listCont; 
    this.activity = activity; 
    this.profileType = profileType; 
} 

public class ViewHolder { 
    View rowView; 
    public TextView textEmailId= null; 
    public TextView textContNo= null; 
    public TextView text= null; 
    public ImageView photo= null; 
    public int position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    //View view = convertView; 
    ViewHolder holder; 

    if (convertView == null) { 
     LayoutInflater inflater = (LayoutInflater) getContext() 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(renderer, null); 
     holder = new ViewHolder(convertView); 
     holder.text = (TextView) convertView.findViewById(R.id.name); 
     holder.textContNo = (TextView) convertView.findViewById(R.id.contactno); 
     holder.textEmailId = (TextView) convertView.findViewById(R.id.emailId); 
     holder.photo = (ImageView) convertView.findViewById(R.id.quickContactBadge1); 
     convertView.setTag(holder); 
    } 
    else { 
     holder =(ViewHolder) convertView.getTag();  
     holder.position = position; 
     Profile contact = listCont.get(position); 
     holder.text.setText(contact.getName()); 
     ImageView photo = (ImageView) convertView.findViewById(R.id.quickContactBadge1); 
     photo.setTag(contact.getMobileNo()); 
     photo.setImageResource(R.drawable.stub_image); 
     new LoadImage(photo).execute(contact.getMobileNo()); 
    } 
    return convertView;  
} 

class LoadImage extends AsyncTask<String, Void, Bitmap>{ 
    // private final WeakReference<ImageView> viewReference; 
    private ImageView img; 

    public LoadImage(ImageView img) { 
     //viewReference = new WeakReference<ImageView>(view); 
     this.img=img; 
    } 

    @Override 
    protected Bitmap doInBackground(final String... params) { 
     new QuickContactHelper(activity, img, (String) params[0]).addThumbnail(); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Bitmap bitmap) { 
     // nothing 
    } 
} 

문제가 내가 스크롤을 시작할 때 이미지 만 스크롤 한 후, 나는 목록을 얻고 있지만, 이미지가로드입니다 잘못하고 어디 스피 확인 안정과 오른쪽 position..not에 , 나는 목록로드 후 이미지를로드해야하고 이미지가 올바른 위치에 있어야합니다 ... 어떤 도움을 주신다면

답변

0

convertView == null이면 아무 것도하지 않는 것 같습니다. if와 else 모두에서 그림을 지정해야합니다.

if (convertView == null) { 
    LayoutInflater inflater = (LayoutInflater) (getContext() 
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)); 
    convertView = inflater.inflate(renderer, null); 

    holder = new ViewHolder(convertView); 
    holder.text = (TextView) convertView.findViewById(R.id.name); 
    holder.textContNo = (TextView) convertView.findViewById(R.id.contactno); 
    holder.textEmailId = (TextView) convertView.findViewById(R.id.emailId); 
    holder.photo = (ImageView) convertView.findViewById(R.id.quickContactBadge1); 

    convertView.setTag(holder); 
}else { 
    holder =(ViewHolder) convertView.getTag();  
} 

holder.position = position; 
Profile contact = listCont.get(position); 
holder.text.setText(contact.getName()); 

ImageView photo = (ImageView) convertView.findViewById(R.id.quickContactBadge1); 
photo.setTag(contact.getMobileNo()); 
photo.setImageResource(R.drawable.stub_image); 

새로운 LoadImage (사진) .execute (contact.getMobileNo());

+0

대답을 자세히 설명해 주실 수 있습니다 ... 빈 이미지 또는 원래 연락처 이미지를 지정 하시겠습니까? – teekib

+0

아니, 그냥 해봐. if 문에있는 코드는 무언가를 지정해야합니다. 내 대답을 편집 할게. – Heinrisch

0

런타임시 어댑터 을 스크롤 할 때 어댑터 inflator에서 이미지를 설정합니다. 액티비티로 이미지를 설정해야합니다.

+0

나는 혼란 스럽다. 더 많은 plzz을 설명 할 수있다. – teekib

+0

새로운 LoadImage (사진) .execute (contact.getMobileNo()); exectue 액티비티 클래스에서이 이미지를 가져 와서 설정하십시오. – QuokMoon

+0

iam은이 모든 것을 프로파일 어댑터 클래스에서로드하는 중이고 activity / – teekib

0

convertview! = null 일 때만 AsynckTask를 시작합니다.

이 그냥 if 문 밖에서이 모든 코드를 삽입 해결하려면 :

holder.position = position; 
    Profile contact = listCont.get(position); 
    holder.text.setText(contact.getName()); 

    ImageView photo = (ImageView) convertView.findViewById(R.id.quickContactBadge1); 
    photo.setTag(contact.getMobileNo()); 
    photo.setImageResource(R.drawable.stub_image); 
    new LoadImage(photo).execute(contact.getMobileNo()); 

이미지 로딩을 위해 내가보기 엔이 라이브러리를 추천합니다 :이 당신을 도와줍니다

Android-Universal-Image-Loader

희망을.

관련 문제