获取适配器类中的ImageView onClick侦听器的位置

时间:2014-03-08 05:16:35

标签: android adapter

我想获得imageView onclick的位置但是我不知道如何实现它。 代码:

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(viewholder.newsCheck.getTag(position));

        }
    });

    return convertView;

}
}

1 个答案:

答案 0 :(得分:3)

您可以使用TAG获取所需的所有内容。

您需要编写以下代码:

viewholder.newsCheck.setTag(position);

然后您可以使用getTag()方法获取您的价值

<强>更新

你需要onClick方法的 v.getTag()来获取位置。

然后你的代码应该是:

    @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);

            viewholder.newsCheck.setTag(position);  // add this line

            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.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
               Log.d("position is" , ""+v.getTag());  // get position of your view

            }
        });

        return convertView;

    }