I have a listview with custom adapter in listfragment and also set onclicklistner for listview. But Onclicklistner does not work.
Here is my code:
p
When you are inflating your views, it seems that your view has 2 Buttons in it:
this row.findViewById(R.id.tips);
and this row.findViewById(R.id.fav);
You should either remove these buttons (replace them with textviews if you do not want their click)
Or if you want the clicks of these buttons, you can use OnClickListeners for each button and another OnClickListener for the whole view.
EXAMPLE
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
//we only inflate if the row is null!!
if(row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
RecipesHolder holder = new RecipesHolder();
holder.imgIcon = (ImageView) row.findViewById(R.id.imageView1);
holder.txtTitle = (TextView) row.findViewById(R.id.title);
holder.category = (TextView) row.findViewById(R.id.category);
holder.source = (TextView) row.findViewById(R.id.source);
holder.country = (TextView) row.findViewById(R.id.country);
holder.readytime = (TextView) row.findViewById(R.id.readytime);
holder.tips = (Button) row.findViewById(R.id.tips);
holder.fav = (Button) row.findViewById(R.id.fav);
//we set the tag of the view to this holder so we can get it everytime
row.setTag(holder);
}
//change this to final so that you can use it inside your click listeners
final Recipes ap = data[position];
//here we get the holder of the view from its tag
RecipesHolder holder = (RecipesHolder) row.getTag();
//no changes to ur setup
imageLoader.DisplayImage(ap.getIMAGENAME240(), holder.imgIcon);
holder.txtTitle.setText(ap.getNAME());
holder.category.setText(ap.getCATEGORY());
holder.source.setText(ap.getSOURCE());
holder.country.setText(ap.getCOUNTRY());
holder.readytime.setText(ap.getREADYTIME());
//now the click listeners
//tips button
holder.tips.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//tips has been clicked
//do whatever you want with `ap`
}
});
//fav button
holder.fav.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//fav has been clicked
//do whatever you want with `ap`
}
});
//whole item click
row.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//the row has been clicked
//do whatever you want with `ap`
}
});
return row;
}