我必须在点击该图像时更改图像。我正在尝试这样做,它正在改变图像,但不是在那个位置,在一个随机位置。 代码
public class InteractiveArrayAdapter extends
ArrayAdapter<HashMap<String, String>> {
public boolean[] checkBoxState;
boolean[] favStar;
ViewHolder viewholder;
// private Object inflater;
private LayoutInflater inflater;
private ArrayList<HashMap<String, String>> menuItems;
public InteractiveArrayAdapter(Context context, int resource,
ArrayList<HashMap<String, String>> menuItems) {
super(context, resource, menuItems);
this.menuItems = menuItems;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
checkBoxState = new boolean[menuItems.size()];
}
private class ViewHolder {
// ImageView photo;
TextView title, link, published, description;
// ImageButton newsCheck;
public ImageView newsCheck;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
// inflater =
// LayoutInflater.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item1, null);
viewholder = new ViewHolder();
// cache the views
// viewholder.photo=(ImageView)
// convertView.findViewById(R.id.photo);
viewholder.title = (TextView) convertView
.findViewById(R.id.news_title);
// viewholder.link = (TextView) convertView.findViewById(R.id.link);
viewholder.published = (TextView) convertView
.findViewById(R.id.published);
viewholder.description = (TextView) convertView
.findViewById(R.id.description);
viewholder.newsCheck = (ImageView) convertView
.findViewById(R.id.newsCheck);
System.out.println("position" + position);
// link the cached views to the convertview
convertView.setTag(viewholder);
} else
viewholder = (ViewHolder) convertView.getTag();
HashMap<String, String> catalog_list = new HashMap<String, String>();
catalog_list = menuItems.get(position);
// viewholder.link.setText(catalog_list.get("NEWS_KEY_LINK")
//
// viewholder.link.setText(catalog_list
// .get(NewsXMLParsingActivity.NEWS_KEY_LINK));
viewholder.title.setText(catalog_list
.get(NewsXMLParsingActivity.NEWS_KEY_TITLE));
viewholder.published.setText(catalog_list
.get(NewsXMLParsingActivity.NEWS_KEY_PUB));
viewholder.description.setText(catalog_list
.get(NewsXMLParsingActivity.NEWS_KEY_DESCRIPTION));
viewholder.newsCheck.setTag(position);
viewholder.newsCheck.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewholder.newsCheck.getTag(position);
System.out.println(v.getTag() + "qqqqqq");
int position = (Integer) v.getTag(); // This will retrieve your
// clicked image
viewholder.newsCheck.setImageResource(R.drawable.favv2);
}
});
return convertView;
}
}
答案 0 :(得分:5)
试试这个......
public class InteractiveArrayAdapter extends
ArrayAdapter<HashMap<String, String>> {
public boolean[] checkBoxState;
boolean[] favStar;
// private Object inflater;
private LayoutInflater inflater;
private ArrayList<HashMap<String, String>> menuItems;
// track the positions of images changed;
private final HashSet<Integer> imageIndexes;
public InteractiveArrayAdapter(Context context, int resource,
ArrayList<HashMap<String, String>> menuItems) {
super(context, resource, menuItems);
this.menuItems = menuItems;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
checkBoxState = new boolean[menuItems.size()];
imageIndexes = new HashSet<Integer>();
}
private static class ViewHolder {
// ImageView photo;
TextView title, link, published, description;
// ImageButton newsCheck;
public ImageView newsCheck;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
ViewHolder viewholder = null;
if (convertView == null) {
// inflater =
// LayoutInflater.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item1, null);
viewholder = new ViewHolder();
// cache the views
// viewholder.photo=(ImageView)
// convertView.findViewById(R.id.photo);
viewholder.title = (TextView) convertView
.findViewById(R.id.news_title);
// viewholder.link = (TextView)
// convertView.findViewById(R.id.link);
viewholder.published = (TextView) convertView
.findViewById(R.id.published);
viewholder.description = (TextView) convertView
.findViewById(R.id.description);
viewholder.newsCheck = (ImageView) convertView
.findViewById(R.id.newsCheck);
System.out.println("position" + position);
// link the cached views to the convertview
convertView.setTag(viewholder);
} else {
viewholder = (ViewHolder) convertView.getTag();
}
HashMap<String, String> catalog_list = new HashMap<String, String>();
catalog_list = menuItems.get(position);
// viewholder.link.setText(catalog_list.get("NEWS_KEY_LINK")
//
// viewholder.link.setText(catalog_list
// .get(NewsXMLParsingActivity.NEWS_KEY_LINK));
viewholder.title.setText(catalog_list
.get(NewsXMLParsingActivity.NEWS_KEY_TITLE));
viewholder.published.setText(catalog_list
.get(NewsXMLParsingActivity.NEWS_KEY_PUB));
viewholder.description.setText(catalog_list
.get(NewsXMLParsingActivity.NEWS_KEY_DESCRIPTION));
viewholder.newsCheck.setTag(position);
if(imageIndexes.contains(Integer.valueOf(position))) {
viewholder.newsCheck.setImageResource(R.drawable.favv2);
} else {
viewholder.newsCheck.setImageResource(R.drawable.favv1);
}
viewholder.newsCheck.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int position = (Integer) v.getTag();
ImageView imageView = (ImageView) v;
imageView.setImageResource(R.drawable.favv2);
imageIndexes.add(Integer.valueOf(position));
}
});
return convertView;
}
}
答案 1 :(得分:3)
当您点击ImageView
时,View v
参数将是ImageView
本身。因此,如果您只需更改图像,则只需将其转换为ImageView
,然后更改其图像资源。
@Override
public void onClick(View v)
{
((ImageView) v).setImageResource(R.drawable.favv2);
}