If you wish that on pressing a particular list items , the list item does not gets selected for example if you wish that last two items in list view can never be selected , its implementation is as follows:
The data shown in ListView is determined by its adapter and all adapter are child of BaseAdapter.
public boolean areAllItemsEnabled()
public boolean isEnabled(int position)
By overriding the above two methods in our custom adapters we can achieve disabling selection of selected items in a ListView
1. The overridden method public boolean areAllItemsEnabled() should return false;
2. In the overridden method public boolean isEnabled(int position) implement returning false for all the list item positions for which you do not wish the list items to be selected.
Below is a example of custom adapter in which the last two list items cannot be selected. the adapter takes data from String array mStrings
class CustomAdapter extends BaseAdapter { /** used to inflate a layout xml as list view item **/ private LayoutInflater mInflater; /** holds the reference of TextView which is part of inflated layout the TextView can be reused for every list item **/ private TextView mTextView; /** initialize layout inflator **/ public CustomAdapter(Context context) { mInflater=LayoutInflater.from(context); } /** returns the length of the data items in source ie mStrings **/ @Override public int getCount() { return mStrings.length; } /** returns the value of mString for a particular position **/ @Override public Object getItem(int position) { return mStrings[position]; } /** returns the index of the data item in source ie mStrings **/ @Override public long getItemId(int position) { return position; } /** returns view for list item **/ @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = mInflater.inflate(R.layout.list_item, null); mTextView=(TextView) convertView.findViewById(R.id.mListItemTextView); convertView.setTag(mTextView); } else { mTextView = (TextView) convertView.getTag(); } mTextView.setText(mStrings[position]); return convertView; } /**the method returns true if all the list items can be selected **/ @Override public boolean areAllItemsEnabled() { return false; } /**the method returns true for a particular list item position so that the list item at that position can be selected **/ @Override public boolean isEnabled(int position) { /* if position is last index or second last index of mStrings then return false */ return (position==mStrings.length-2|position==mStrings.length-1)?false:true; } } /** data source **/ private String[] mStrings = { "Home", "Mind", "pirates", "Cats", "Arabia", "Fire", "Abundance", "happy", "hope", "Sorrow", "Alien", "Bugs", "pity", "conflict", "internet", "email", "facebook", "Twitter", "legacy", "Java", " ", " " }; the layout of list_item.xml is as follows: