
我必须在点击该图像时更改图像.我正在尝试这样做,它正在改变图像,但不是在那个位置,在一个随机位置.
码
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;}@OverrIDepublic 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适配器中替换单击该图像时的图像全部内容,希望文章能够帮你解决android – 在listview适配器中替换单击该图像时的图像所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)