I am having custom adapter which extends ArrayAdapter
. I am having different layouts for different items. I like to make items that are from particular type unclickable
. Here is my code:
package android.adapters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Switch;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import android.items.ItemProperties;
public class CustomAdapter extends ArrayAdapter<ItemProperties> {
private static final int TYPE_ITEM_AMOUNTS = 0;
private static final int TYPE_ITEM_LIMITS = 1;
private static final int TYPE_MAX_COUNT = 2;
private LayoutInflater mInflater;
private ArrayList<Integer> positions = new ArrayList<Integer>();
private List<ItemProperties> items = new ArrayList<ItemProperties>();
public CustomAdapter(Context context, int resource, List<ItemProperties> itemCards, ArrayList positions) {
super(context, resource, items);
this.positions = positions;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public ItemProperties getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflaterRow = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = null;
int type = getItemViewType(position);
switch (type) {
case TYPE_ITEM_AMOUNTS:
rowView = inflaterRow.inflate(R.layout.item_details, parent, false);
rowView.setEnabled(false);
rowView.setOnClickListener(null);
TextView txtName = (TextView) rowView.findViewById(R.id.text_name);
txtName.setText(itemCards.get(position).getCardLabelDesc());
TextView txtValue = (TextView) rowView.findViewById(R.id.text_value);
txtValue.setText(itemCards.get(position).getCardLabelValue());
break;
case TYPE_ITEM_LIMITS:
rowView = inflaterRow.inflate(R.layout.item_limit_settings, parent, false);
rowView.setEnabled(true);
TextView txtLimitDesc = (TextView) rowView.findViewById(R.id.text_item_description);
txtLimitDesc.setText(itemCards.get(position).getCardLabelDesc());
Switch limitEnable = (Switch) rowView.findViewById(R.id.enable_limit);
limitEnable.setText("");
TextView txtLimit = (TextView) rowView.findViewById(R.id.txt_limit);
txtLimit.setText(itemCards.get(position).getCardLabelValue());
break;
}
return rowView;
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
@Override
public int getItemViewType(int position) {
if (positions.contains(position))
return TYPE_ITEM_LIMITS;
else
return TYPE_ITEM_AMOUNTS;
}
}
However, this does not work for me, nothing is clickable.
In the activity in which I populate the list I have this:
propertiesAdapter=new
CustomAdapter(this,R.layout.item_details,itemPropertiesList,positions);
listDetails.setAdapter(propertiesAdapter);
listDetails.setOnItemClickListener(this);
and I have onItemClick
event handler.
Does anybody knows what is the problem?
In the type of TYPE_ITEM_AMOUNTS,you set the view to be disabled,and OnClickListener null,so it's not clickable,I guess this is exactly what you want;
In the type of TYPE_ITEM_LIMITS,you have a Switch here,but you don't set a OnCheckedChangeListener for the switch. When you using a OnItemClickListener for a ListView,remember this:If there is anything that can be clicked in your list item(a button,or in your case, a switch),then the onItemClick() method will never work.This is why nothing is clickable in your listView.
You should set all the views in your list not focusable so that onItemClick() can work normally.Or another way ,do not use OnItemClickListener ,just set a OnCheckedChangeListener for your switch to handle click event.