Make different clickable views on a listview item in android

Deadly 提交于 2019-12-05 06:27:21

问题


In my application i have a listView. each item of the list view contains a clickable main body(image and some text) and 3 other textViews which are clickable.

when i click on the item it shows the map of the respective area shown on the item, this works fine because i am using onItemClick on the listview in my activity.

But when i want to implement the onClick on the 3 other TextViews marked red in the image i have to implement the onClick in the getView method of the adapter class.

Here comes the problem:

public View getView(int position, View convertView, ViewGroup parent) {

    View vi = convertView;
    ViewHolder holder = new ViewHolder();
    p = values.get(position);
    String date = new java.text.SimpleDateFormat("dd/MM/yy")
            .format(new java.util.Date(p.timeStamp));
    if (vi == null) {

            vi = inflater.inflate(R.layout.feed_items, null);
            holder.text = (TextView) vi.findViewById(R.id.label);
            holder.image = (ImageView) vi.findViewById(R.id.logo);
            holder.thankLabel = (TextView) vi.findViewById(R.id.thankLabel);
            holder.iwantLabel = (TextView) vi.findViewById(R.id.iWantLabel);
            holder.detailsLabel = (TextView) vi
                    .findViewById(R.id.detailsLabel);

                    holder.thankLabel
                    .setOnClickListener(new View.OnClickListener() {

                        public void onClick(View arg0) {

                        }
                    });
            holder.iwantLabel
                    .setOnClickListener(new View.OnClickListener() {

                        public void onClick(View arg0) {

                            Intent intent = new Intent(activity,
                                    IWantActivity.class);
                            intent.putExtra("productDetails", p.productName
                                    + "^" + p.reportedPrice);
                            activity.startActivity(intent);
                        }
                    });
            holder.detailsLabel
                    .setOnClickListener(new View.OnClickListener() {

                        public void onClick(View arg0) {

                        }
                    });
        }
        vi.setTag(holder);
    } else {

        holder = (ViewHolder) vi.getTag();
    }
    return vi;
}

in my activity when i want the reference of which item is clicked i can use the position parameter, but in case of getView the parameter position gives the newly created item but not the item's iWant i clicked. how to solve this??


回答1:


You can put tags on the labels, and use it as the context for the click handler.



来源:https://stackoverflow.com/questions/11559575/make-different-clickable-views-on-a-listview-item-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!