0

I've prepared a custom listview using BaseAdapter. Now I want to change color of selected item of listview on click event. And multiple items should be selected. Here I am giving one demo :

enter image description here

Selected item's color is Orange. This is just a demo screen. If anybody knows to how change entire background color of a selected list item then please post their reviews. Thanks.

I am using this BaseAdapter Class:

public class MyListAdapter extends BaseAdapter {

private Activity activity;
private String[] title, artist, duration, rowNumber;
private static LayoutInflater inflater=null;
ViewHolder holder;

View vi;

public MyListAdapter (Activity context, String[] songTitle,String[] songArtist, String[] songDuration )
{
   try
   {
    activity = context;
    title = songTitle;
    artist = songArtist;
    duration = songDuration;

    rowNumber = new String[title.length];
    for(int i=0;i<title.length; i++){
        rowNumber[i] = Integer.toString(i+1);
    }
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

   }
   catch (NullPointerException e) 
   {
       e.printStackTrace();
   }
}

public int getCount() {
    return title.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public static class ViewHolder{
    private TextView txtSongNumber, txtSongTitle, txtSongArtist, txtSongDuration;

}

public View getView(int position, View convertView, ViewGroup parent) 
{
    try
    {
        vi=convertView;

       // System.out.println("Value of position"+position);
        if(convertView==null)
        {
            vi = inflater.inflate(R.layout.list_songs, null);
            holder=new ViewHolder();
            holder.txtSongNumber = (TextView)vi.findViewById(R.id.txtSongNumber);
            holder.txtSongTitle = (TextView)vi.findViewById(R.id.txtSongTitle);
            holder.txtSongArtist = (TextView)vi.findViewById(R.id.txtSongArtist);
            holder.txtSongDuration = (TextView)vi.findViewById(R.id.txtSongDuration);

            vi.setTag(holder);
        }
        else

            holder=(ViewHolder)vi.getTag();

        holder.txtSongNumber.setText(rowNumber[position]);
        holder.txtSongTitle.setText(title[position]);
        holder.txtSongArtist.setText(artist[position]);

        holder.txtSongDuration.setText(duration[position]);

    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }
    return vi;
}

}

Krishna Suthar
  • 3,071
  • 6
  • 31
  • 37
  • There are lot question regarding to your query. [Anyone of the link may helps you](http://www.google.com/search?q=android+listview+background&ie=utf-8&oe=utf-8&aq=t#hl=en&sclient=psy-ab&q=android+listview+background+color+change&oq=android+listview+background+color+change&aq=f&aqi=g-K2g-bK1g-bsK1&aql=&gs_l=serp.3..0i30l2j0i8i30j0i8i10i30.6102.9207.0.9318.15.12.1.2.2.1.509.2396.0j5j3j1j0j1.10.0...0.0.sK4QGOSwbEE&pbx=1&bav=on.2,or.r_gc.r_pw.r_qf.,cf.osb&fp=71438d7011881060&biw=1440&bih=785) – Praveenkumar May 23 '12 at 05:32

3 Answers3

2

Its very simple... Just try the following code...

In your List Adapter:

Define an Integer Array first

ArrayList<Integer> itemPos = new ArrayList<Integer>();

then use this code in your getView Method :

        if (itemPos.contains(position)) {
            holder.txtOne.setTextColor(Color.BLUE);
        } else {
            holder.txtOne.setTextColor(Color.WHITE);
        }

Now use this code in click event of your Text View :

            if (!itemPos.contains(position)) {
                holder.txtOne.setTextColor(Color.BLUE);
                itemPos.add(position);
                notifyDataSetChanged();
            } else {
                holder.txtOne.setTextColor(Color.WHITE);
                notifyDataSetChanged();
                int po = itemPos.indexOf(position);
                itemPos.remove(po);
            }
Vipul Purohit
  • 9,807
  • 6
  • 53
  • 76
0

Since you've implemented BaseAdapter, which has a core method called getView, you can easily store the states of items in BaseAdapter. For example, you can use List to store the states.

Then, a listener of the ListView should be implemented, as described in http://developer.android.com/reference/android/widget/AdapterView.html#setOnClickListener(android.view.View.OnClickListener). The documentation of OnItemClickListener is here, http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html, through which you can get the positions of the clicks.

Finally, change the state of the items after on-click events, and call notifyDataSetChanged() in adapter to notify that the data has been changed and you'll see the updated selected items.

Davidsun
  • 721
  • 6
  • 13
  • Thanks a lot for your help. I've done all this. Now I found that when I click one item in list, another item is also get selected automatically. Is there any problem with BaseAdapter? – Krishna Suthar May 23 '12 at 05:50
  • No, BaseAdapter works well. But you need to check your code to make sure that you've used right parameter of OnItemClickListener. In OnItemClickListener, position means "the position of the view in the adapter", as described in the documentation. – Davidsun May 23 '12 at 05:56
  • I am getting selected item position. Its correct. But why it is selecting more than one items when I select only one? – Krishna Suthar May 23 '12 at 06:00
  • You can try to use Log.w to print how many times that onItemClick(...) is called, as well as the positions of clicked items. – Davidsun May 23 '12 at 06:11
0

"Thanks a lot for your help. I've done all this. Now I found that when I click one item in list, another item is also get selected automatically. Is there any problem with BaseAdapter?"

For this problem you need to save states of your listview rows that whether that row is selected or not in getview check if it is selected then set color of row as selected. You need to save state also whenever you select or deselect any row. Here is a similar example instead of checkbox, row will be there in your case... Hope it will help you i am giving you a link ....

How to implement a button that gets all checkbox's state and adds the value of checked item into arraylist?

Community
  • 1
  • 1
Bharat Sharma
  • 3,926
  • 2
  • 17
  • 29