3

I have a simple array of Strings that I was displaying in a horizontal ListView with an ArrayAdapter. What I'm looking to do is: when the user selects an item from the ListView, make that item not clickable and change the background color of that item. Perhaps like a "grayed-out" look to it. I was looking into creating a custom Adapter and overriding the isEnabled(int position) method but I don't know how I would go about this. Any advice, suggestions, or help will be greatly appreciated thanks!

chRyNaN
  • 3,592
  • 5
  • 46
  • 74

4 Answers4

9

I was looking into creating a custom Adapter and overriding the isEnabled(int position) method but I don't know how I would go about this.

This is quite easy to do. I recommend a SparseBooleanArray to track the enabled items for efficiency:

public class MyAdapter extends ArrayAdapter<String> {
    private SparseBooleanArray enabledItems = new SparseBooleanArray(); 

    public MyAdapter(Context context, int textViewResourceId, List<String> objects) {
        super(context, textViewResourceId, objects);
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return enabledItems.get(position, true);
    }

    public void toggleItem(int position) {
        boolean state = enabledItems.get(position, true);
        enabledItems.put(position, !state);
    }
}

The AutoComplete feature of Eclipse did must of the work, but here are some quick notes:

  • You must override areAllItemsEnabled() along with isEnabled()
  • I designed toggle() to be used by an onItemClickListener() you only need to call adapter.toggle(position)
  • If you want to change the row's appearance (more than what enabling and disabling does by default) simply override getView(). Don't forget to cover both cases:

    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = super.getView(position, convertView, parent);
    
        if(!isEnabled(position)) { 
            /* change to disabled appearance */ 
        } 
        else { 
            /* restore default appearance */ 
        }
        return convertView;
    }
    

    Hope that helps!

Sam
  • 86,580
  • 20
  • 181
  • 179
2

pass position to adapter class when you click on list item

 @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        adapter.setSelectedIndex(position);
    }

add method of setSelectedIndex to adapter class

  public void setSelectedIndex(int ind)
    {
        selectedIndex = ind;
        notifyDataSetChanged();
    }

Now check the postion of this listview if same then enable and disable value in getView me method

 if(selectedIndex!= -1 && position == selectedIndex)
        {
            holder.tv.setBackgroundColor(Color.BLACK);
        }
        else
        {
            holder.tv.setBackgroundColor(selectedColor);
        }
        holder.tv.setText("" + (position + 1) + " " + testList.get(position).getTestText());

Reference from here

Community
  • 1
  • 1
QuokMoon
  • 4,387
  • 4
  • 26
  • 50
0

Use setEnabled(bool) property:

yourlistview.setEnabled(false);
Ali
  • 2,012
  • 3
  • 25
  • 41
  • Will this disable the entire ListView? Because I only need it to disable the clicked item within the ListView. – chRyNaN Apr 08 '13 at 18:21
0

Not sure whether it will work or not

    public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
                // your code
                view.setBackgroundColor(Color.BLUE);
                view.setEnabled(false);
    }
Kaidul
  • 15,409
  • 15
  • 81
  • 150
  • This would work if each row was a separate View, but ListViews recycle rows for efficiency so this won't behave as you expect. – Sam Apr 08 '13 at 18:29