1

I have a listview that is populated via an adapter. I need one of the items (which are just bits of text) in the listview to have a different background compared to the others (to mark it as the currently selected one). I have all of the background logic, I just need a way to say listview.setBackgroundById(int position) or something like that.

How do I do this? This needs to be as simple as possible, 'cause all of the other things done in the Activity are currently working perfectly. :)

As asked, this is my Adapter that I'm using:

   public class SampleAdapter extends ArrayAdapter<SampleItem> {
    private String title;

    public SampleAdapter(Context context) {
        super(context, 0);
    }

    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_station, null);
        }

        TextView title = (TextView)convertView.findViewById(R.id.station_name);
        font.setFont(title, 2, getActivity());
        title.setText(getItem(position).title);

        RelativeLayout info = (RelativeLayout)convertView.findViewById(R.id.info_relative_button);
        info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.setCurrentTab(41);
                MainActivity.setBackDisabled(true);
                Log.e("current tab:",String.valueOf(MainActivity.getCurrentTab()));
                Fragment fragment = new StationInfo();
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
                UserManager.getInstance().setStationId(getItem(position).id);
            }
        });



        return convertView;
    }

}

The SampleItem has 2 String fields, title and id, it's very simple.

Orlando Bloom
  • 109
  • 4
  • 10

2 Answers2

0

You need to use a custom list adapter and have it return views with your desired background. Create a class extending ListAdapter or any of the existing SimpleAdapter etc and override getView to inflate a suitable view for your element, and add any logic you need to set the background of that view. There is no way to tell the listview itself to decorate some of its elements by id or position.

Update: I just noticed you added the list adapter code. Since you are already implementing getView, to change the background of your element simply call convertView.setBackgroundColor, or have two different views inflated depending on the situation.

(BTW it's really bad practice to call static methods on your activity like in your onClickListener.)

JHH
  • 8,567
  • 8
  • 47
  • 91
0

In ListView adapter:

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {

        if(view==null)
            ....

        //for example every even list item to be grey, every odd to be white
        if(((position+1)%2)==0)
            view.setBackgroundColor(mContext.getResources().getColor(R.color.grey));
        else  view.setBackgroundColor(mContext.getResources().getColor(android.R.color.white));

Hope you get an idea...

Šime Tokić
  • 700
  • 1
  • 9
  • 22