Reputation: 9013
I have a row file for my listview which has 3 elements. A name on the left side of the row. An Imageview(Right Arrow which indicates going to next page) and a main LinearLayout in which i have placed these items.
Here is my getView() method of my adapter..
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView=LayoutInflater.from(context).inflate(R.layout.activity_row_mail_category,null);
holder=new Holder();
setView(convertView,position);
convertView.setTag(holder);
}
else {
holder= (Holder) convertView.getTag();
setView(convertView,position);
}
holder.Name.setText(mailCategoryArrayList.get(position).getCat_name());
return convertView;
}
Holder is a static class which i have made in the adpater itself.
static class Holder{
TextView Name;
ImageView next;
LinearLayout mainLayout;
}
Here is the setView method which i called from getView
private void setView(final View convertView,final int position) {
holder.Name = (TextView) convertView.findViewById(R.id.listview_row_title);
holder.next = (ImageView) convertView.findViewById(R.id.next);
holder.main = (LinearLayout) convertView.findViewById(R.id.main);
holder.next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
Log.e("tag","Position:"+position);
}
});
holder.mailCategoryMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.next.performClick();
});
Whenever i run the app and click on the layout i get the position as the last index of my arrayList but when i click the imageview i get the correct position. So what is the problem with performClick() function and why does this occur when i am simply telling android to click the image view programatically instead of manually?
Upvotes: 0
Views: 923
Reputation: 2618
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if(convertView==null){
convertView=LayoutInflater.from(context)
.inflate(R.layout.activity_row_mail_category,null);
holder=new Holder();
holder.Name.setText(mailCategoryArrayList.get(position).getCat_name());
holder.Name = (TextView) convertView.findViewById(R.id.listview_row_title);
holder.next = (ImageView) convertView.findViewById(R.id.next);
holder.main = (LinearLayout) convertView.findViewById(R.id.main);
convertView.setTag(holder);
}else {
holder= (Holder) convertView.getTag();
}
holder.next.setTag(position);//To avoid finalize
holder.next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
Integer pos=(Integer)view.getTag();
Log.e("tag","Position:"+pos);
}
});
holder.mailCategoryMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.next.performClick();
});
return convertView;
}
Upvotes: 0
Reputation: 1936
Initialze your holder
inside setView()
function will solve the problem
private void setView(final View convertView,final int position) {
Holder holder = new holder();
holder.Name = (TextView) convertView.findViewById(R.id.listview_row_title);
holder.next = (ImageView) convertView.findViewById(R.id.next);
holder.main = (LinearLayout) convertView.findViewById(R.id.main);
holder.next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
Log.e("tag","Position:"+position);
}
});
holder.mailCategoryMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.next.performClick();
});
on your code you are using the same instance
of holder
and it saves the last value, hope this helps
Upvotes: 0