Search code examples
androidlistviewandroid-custom-view

android set specific rows background of a listview with a color


I am working on a listview, and would like to higlight, for example, row 3 to row 7 in the listview.

Code:

@Override
public void onCreate(Bundle savedInstanceState) 
{   
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sample_list);
    lv = (ListView) findViewById(R.id.sample_listview);

    String[] Sample_List = getResources().getStringArray(R.array.Sample_List);  
    final ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < Sample_List.length; ++i) 
    {
        list.add(Sample_List[i]);
    }

    final StableArrayAdapter adapter = new StableArrayAdapter(this, R.layout.list_item, list);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            ...
        }
    });
}

updates:

I have updated the codes as follows:

    lv.setAdapter(myAdapter);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            ... 
        }
    });
}

public class MyCustomArrayAdapter extends ArrayAdapter<String> 
{
      private final Context context;
      private final String[] values;

      public MyCustomArrayAdapter(Context context, String[] values) 
      {
        super(context, R.layout.list_item, values);
        this.context = context;
        this.values = values;
      }

      @Override
      public View getView  (int position, View convertView, ViewGroup parent)
      {
          View  view = super.getView(position, convertView, parent);
          if(position>=3&&position<=7)
          {
              view.setBackgroundResource(R.drawable.green_btn);
          }
          else
          {
              view.setBackgroundResource(R.drawable.white_btn);
          }  
          return view;
      }
} 

Question:

I have researched a lot on the web but I still cannot figure out how to implement that getView... in this way how could the above be changed such that for example, background row 3 to 7 to be set in red?

Implemented the updated codes, the listview cannot expand properly to show each item, only item's border can be shown (the green button has some black border), possibly because not linking the list.

How could I connect the list with the MyCustomArrayAdapter?


Solution

  • What about storing the highlighted positions in a List, so that you can do something like this.-

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        if (highlightedPositions.contains(position)) {
            view.setBackgroundColor(yourColor);
        }
        return view;
    }
    

    EDIT

    Now that you have your own custom adapter, replace the code where you're setting the StableArrayAdapter.-

    final StableArrayAdapter adapter = new StableArrayAdapter(this, R.layout.list_item, list);
    lv.setAdapter(adapter);
    

    with this.-

    final MyCustomArrayAdapter adapter = new MyCustomArrayAdapter(this, list);
    lv.setAdapter(adapter);