gpt4 book ai didi

android - 在 ListView 适配器中,单击该图像替换图像

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:02 24 4
gpt4 key购买 nike

我必须在单击该图像时更改图像。当我尝试这样做时,它正在更改图像,但不是在那个位置,而是在一个随机位置。代码

     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;

}

最佳答案

试试这个...

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;

}
}

关于android - 在 ListView 适配器中,单击该图像替换图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22317700/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com