【发布时间】:2014-03-29 09:53:03
【问题描述】:
一旦用户单击列表项中的图像,我将尝试更改 ListFragment 中的图像。我必须实现一个自定义列表适配器,因为我必须在运行时加载图像。单击图像时列表视图图像会发生变化,但在我滚动列表时,相同位置的其他项目也会发生变化。 请帮忙。
我的适配器代码如下:
public class PeopleAdapter extends BaseAdapter{
private FindPeople activity;
private ArrayList<HashMap<String, String>> data=new ArrayList<HashMap<String,String>>();
private static LayoutInflater inflater=null;
private static boolean imageLoadFinish=false;
private final Object semaphore = new Object();
ImageView thumb_image;
public PeopleAdapter(FindPeople followedActivities, ArrayList<HashMap<String, String>> d) {
// TODO Auto-generated constructor stub
activity = followedActivities;
data.addAll(d);
inflater = (LayoutInflater)activity.getActivity().getLayoutInflater();//getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView( final int position, View convertView, final ViewGroup parent) {
// TODO Auto-generated method stub
Log.d("getView", "getView Entered");
final ViewHolder holder;
imageLoadFinish=false;
View vi=convertView;
if(convertView==null){
convertView = inflater.inflate(R.layout.found_people_row, null); //This should be your row layout
holder = new ViewHolder();
holder.name = (TextView)convertView.findViewById(R.id.Heading); // title
holder.avatar=(ImageView)convertView.findViewById(R.id.TypeIcon); //image
holder.favIcon = (ImageView)convertView.findViewById(R.id.followIcon);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
HashMap<String, String> localhash = new HashMap<String, String>();
localhash = data.get(position);
String currenttitle = localhash.get("Name");
String imagepath = localhash.get("avatar");
holder.name.setText(currenttitle);
if(!imagepath.equals(""))
{
holder.avatar.setImageBitmap(FollowedActivities.memoryCache.get(imagepath));
}
holder.favIcon.setTag(position);
holder.favIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("Tag Position", ""+v.getTag());
holder.favIcon.setImageResource(R.drawable.favorite_512);
}
});
return convertView;
}
static class ViewHolder {
ImageView avatar;
TextView name;
ImageView favIcon;
}
}
【问题讨论】:
标签: android list android-fragments android-fragmentactivity listadapter