简体   繁体   中英

How to update image in listview for android

I want to change the image in ImageView in ListView but it is not working.

Bellow is my data and list adapter.

public class Archivedata {
    public String mTo = "";
public ImageView mImage;
public int mID = 0;
public String mText = "";
public String mDate;

}



public class ArchiveListAdapter extends BaseAdapter{
    private LayoutInflater mInflater;
private Vector<Archivedata> mArchiveVector;


public ArchiveListAdapter(Context context, final Vector<Archivedata> data) {
    mArchiveVector = data;
    mInflater = LayoutInflater.from(context);
}

public int getCount() {
    return mArchiveVector.size();
}

public Object getItem(int position) {
    return mArchiveVector.elementAt(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
            if (convertView == null) {
        convertView = mInflater.inflate(R.layout.archive, null);
                holder = new ViewHolder();
        holder.mTxtArchiveDate = (TextView) convertView.findViewById(R.id.date);
        holder.mTxtArchiveText = (TextView) convertView.findViewById(R.id.txtArchiveText);
            holder.mImage = (ImageView) convertView.findViewById(R.id.list_image);
        holder.mTxtArchiveTo = (TextView) convertView.findViewById(R.id.toname);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    Archivedata data = mArchiveVector.elementAt(position);

    holder.mTxtArchiveDate.setText(data.mDate);
    holder.mTxtArchiveText.setText(data.mText);         
    holder.mImage.setImageResource(R.drawable.anonymous_logo);
    holder.mTxtArchiveTo.setText(data.mTo);
        return convertView;
}

    private class ViewHolder {
     TextView mTxtArchiveTo;
     ImageView mImage;
     TextView mTxtArchiveText;
     TextView mTxtArchiveDate;
}

}

Here is the code in the activity for updating the image:

private Archives mArchive;
private ArchiveListAdapter mArchiveListAdapter;

for (int i = 0; i < mArchive.mArchive.size(); ++i) {
    Bitmap im = getFacebookPhoto(mArchive.mArchive.elementAt(i).mTo);   
    mArchive.mArchive.elementAt(i).mImage.setImageBitmap(im);
}
mArchiveListAdapter = new ArchiveListAdapter(getActivity(),mArchive.mArchive);
mArchiveListAdapter.notifyDataSetChanged();
mScrollView.setAdapter(mArchiveListAdapter);

It is returning an error on the following row:

mArchive.mArchive.elementAt(i).mImage.setImageBitmap(im);

Can somebody help me please ?

Thank you

I think the order is wrong:

init:

mArchiveListAdapter = new ArchiveListAdapter(getActivity(),mArchive.mArchive);
mScrollView.setAdapter(mArchiveListAdapter);

update:

for (int i = 0; i < mArchive.mArchive.size(); ++i) {
    Bitmap im = getFacebookPhoto(mArchive.mArchive.elementAt(i).mTo);   
    mArchive.mArchive.elementAt(i).mImage.setImageBitmap(im);
}
if(mArchiveListAdapter!=null){
    mArchiveListAdapter.notifyDataSetChanged();
} else {
    //fallback
     mArchiveListAdapter = new ArchiveListAdapter(getActivity(),mArchive.mArchive);
     mScrollView.setAdapter(mArchiveListAdapter);
}

Might have given you some suggestions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM