I am trying to write a BaseAdapter code where i want to get ViewHolder Tag id on each onClickListener , but in each index i am not getting the ViewHolder tag id , it is always giving the first ViewHolder tag id. If there will be 10 items then , if i click on 3rd item then also in the Toast message i am getting the Id from the first one. But in the TextView the id is displaying correctly.
MyCode :
public class MyAdapter extends BaseAdapter{
Activity activity;
Context context;
ArrayList<String> dataList;
public MyAdapter(Activity activity, Context context,ArrayList<String> dataList)
{
this.activity = activity;
this.context = context;
this.dataList = dataList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return dataList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return dataList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return dataList.size();
}
public class ViewHolder {
TextView data_txt
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
holder = new ViewHolder();
if(convertView==null){
convertView = mInflater.inflate(R.layout.data_layout, parent,false);
holder.data_txt = (TextView) convertView.findViewById(R.id.data);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.data_txt.setText(""+holder);
holder.data_txt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, ""+holder, Toast.LENGTH_SHORT).show();
}
});
return convertView;
} }
So , my problem is how can i get the particular holder id in the Toast message. Please let me know , suggest me some good solution.