本文介绍了在列表视图适配器替换对图像的点击图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要改变上image.whan我尝试这样做,它正在改变形象而不是在那个位置的点击图像,在任意位置。
code

I have to change image on click of that image.whan I am trying to do that it is changing image but not at that position,at a random position.Code

     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;

    }
}

这篇关于在列表视图适配器替换对图像的点击图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:33