开发者

get list item postion using image in viewholder event android

I am using a listview having imageviews and textviews. When one of the image views is clicked, I should get the position of a p开发者_Python百科articular listitem.

I know I can get the listitem position on setting clicklistener on a listview, but the image has a view holder item in it, so it's not getting the perfect value, only -1 every time.

get list item postion using image in viewholder event android

Without using getview

So I have tried rightimg.setOnClickListener, but it is not possible to get the position from that.

How can I solve this problem?


Try implementing it like:

public View getView(int position, View convertView, ViewGroup viewGroup)
{
    View row = convertView;
    ViewHolder holder;
    if(row == null)
    {
        row = LayoutInflater.from(mContext).inflate(R.layout.row_branch, null);
        holder=new ViewHolder();
        /**.....Your other implementaion***/
        holder.rightimg = (ImageView)row.findViewById(R.id.image);
        row.setTag(holder);
    }
    else
        holder = (ViewHolder)row.getTag();

    final int pos = position;
    holder.rightimg.setOnClickListener(new OnClickListener()
    {
        public void onClick(View view)
        {
            //Here you can implement your logic
            //Here the correct pos is available
        }
    });

    return row;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜