-1

I'm new to android development and i am facing an issue, I have a listview that is filled using an Array adapter, i want to make odd rows (items) in the listview has the color of 'red' for example and the even rows have the color of 'yellow'! how can i achieve this?

here is the code onCreate() method:-

//defining list view
    listView = (ListView)findViewById(R.id.listView);
    //defining data array list to store retrieved data from database
    data = new ArrayList<String>();
    adapter=new ArrayAdapter<String(this,android.R.layout.simple_list_item_1, data);
    listView.setAdapter(adapter);

onPostExecute() method:-

protected void onPostExecute(ArrayList<ProductionCommentsTable> result) {
        // TODO Auto-generated method stub
        for (int i = 0; i < result.size(); i++) {       
            data.add("Date: " + result.get(i).getDate().substring(0, 10) + newline + newline +
                    "Item: " + result.get(i).getItem() + newline + newline +
                    result.get(i).getComments());
                if ( i % 2 == 0) {
                    listView.setBackgroundColor(Color.RED);
                } else {
                    listView.setBackgroundColor(Color.YELLOW);
                }
        }
        adapter.notifyDataSetChanged();

Thanks in advance...

user3804193
  • 113
  • 4
  • 11

3 Answers3

3

you need to create a custom adapter with custom child layout.

in getView(...) method you have to write your logic like.

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = mInflater.inflate("your layout", parent, false);
        holder.backgroundView= (RelativeLayout) convertView.findViewById("Your Background View");
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

           //Your color logic
           if ( position % 2 == 0) {
                holder.backgroundView.setBackgroundColor(Color.RED);
            } else {
                holder.backgroundView.setBackgroundColor(Color.YELLOW);
            }   
    return convertView;
}

Above code is just for reference not the complete one, change it as your needs

Nitesh
  • 3,868
  • 1
  • 20
  • 26
  • thanks for your reply but how can i call this `getView()` method in my `onPostExecute()` method? – user3804193 Aug 12 '15 at 08:05
  • no need to call just make a class which extends BaseAdapter do your things there and set this adapter to your listview. Reference Link for custom adapter implementation http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92 – Nitesh Aug 12 '15 at 08:08
1
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
                (this, android.R.layout.simple_list_item_1, fruits_list){
            @Override
            public View getView(int position, View convertView, ViewGroup parent){
                // Get the current item from ListView
                View view = super.getView(position,convertView,parent);
                if(position %2 == 1)
                {
                    // Set a background color for ListView regular row/item
                    view.setBackgroundColor(Color.parseColor("#FFB6B546"));
                }
                else
                {
                    // Set the background color for alternate row/item
                    view.setBackgroundColor(Color.parseColor("#FFCCCB4C"));
                }
                return view;
            }
        };
Amine
  • 9
  • 1
  • 1
  • Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation would greatly improve its long-term value](//meta.stackexchange.com/q/114762/350567) by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – iBug Jan 29 '18 at 14:19
0

This is how you do it: First you create a CustomList class that extends BaseAdapter:

public class ListCompaniesAdapter extends BaseAdapter {
public List<String> items;
Context c;
public ListCompaniesAdapter(Context c,List<String> items){
    this.c=c;
    this.items=items;
}
@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int position) {
    return items.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                c.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        // Your layout here should be RelativeLayout with a textview as a    child
        convertView = mInflater.inflate(R.layout.industry_item, null);
    }
    title=(TextView) convertView.findViewById(R.id.name);
    title.setText(items.get(position));
    if ( position % 2 == 0) {
            convertView.backgroundView.setBackgroundColor(Color.RED);
        } else {
            convertView .backgroundView.setBackgroundColor(Color.YELLOW);
        }   
    return convertView;
}
}

Now in your post execute this is how you do it :

protected void onPostExecute(ArrayList<ProductionCommentsTable> result) {
    // TODO Auto-generated method stub
    for (int i = 0; i < result.size(); i++) {       
        data.add("Date: " + result.get(i).getDate().substring(0, 10) + newline + newline +
                "Item: " + result.get(i).getItem() + newline + newline +
                result.get(i).getComments());
    }
    adapter.notifyDataSetChanged();

This is the onCreate:

listView = (ListView)findViewById(R.id.listView);
//defining data array list to store retrieved data from database
data = new ArrayList<String>();
adapter=new ListCompaniesAdapter(this,data);
listView.setAdapter(adapter);
Miriana Itani
  • 865
  • 9
  • 25
  • thanks a lot for your help, i am just confused with `backgroundView` variable!! – user3804193 Aug 12 '15 at 08:18
  • In your xml you have a relativelayout give it an id (let us say the id is background) and in your custom adapter do this : backgroundView=(RelativeLayout) convertView.findViewById(R.id.background);. In this way you have access to the background of the items of the listview – Miriana Itani Aug 12 '15 at 08:34