selvam
selvam

Reputation: 1197

How can i change the background color of each items in listview?

I am new guy for android.

I done take my data from database and display it to the listview..

Now i want to set the background color for each item.

That is i retrive data from database, here one field is there, like status..

if the status is 1, then item color will be change to green.

like

Display Listview

How can i do this.

It is possible. Please help me.

Thanks is advance.

Upvotes: 1

Views: 1357

Answers (5)

Jeffrey Klardie
Jeffrey Klardie

Reputation: 3028

Inside the getView() method of your adapter, you can set the background resource based on the amount, as follows:

// assume view is your item view, status the number, and you have a context
if(status  == 1){
    view.setBackgroundColor(context.getResources().getColor(R.color.green));
} else {
    view.setBackgroundColor(context.getResources().getColor(R.color.red));
}

Now you need to make sure to define those colors in your resources by creating a file colors.xml with these contents:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#ff0000</color>
    <color name="green">#00ff00</color>
</resources>

Note that if the items are clickable, it is important to provide feedback to the user when he clicks. In this case, you should use a state list drawable.


Edit after comment. Your adapter should look like this, assuming that Item contains the name and status.

public class MyArrayAdapter extends ArrayAdapter<Item> {

    private final Context context;

    public MyArrayAdapter(Context context, int textViewResourceId, Item[] objects) {
        super(context, textViewResourceId, objects);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item, parent, false);

            holder = new ViewHolder();
            holder.container = (LinearLayout) convertView.findViewById(R.id.container);
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.status = (TextView) convertView.findViewById(R.id.status);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.name.setText(getItem(position).name);
        holder.status.setText(getItem(position).status);

        Item item = getItem(position);
        if(item.status  == 1){
            holder.container.setBackgroundColor(context.getResources().getColor(R.color.green));
        } else {
            holder.container.setBackgroundColor(context.getResources().getColor(R.color.red));
        }

        return convertView;
    }

    private class ViewHolder {
        public TextView name;
        public TextView status;
        public LinearLayout container;
    }
}

Next, your list_item.xml layout should look like this:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0px"
    android:layout_height="wrap_content"
    android:id="@+id/container">

    <TextView
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/name" />

    <TextView
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/status" />

</LinearLayout>

Upvotes: 1

Amit Gupta
Amit Gupta

Reputation: 8939

Create a Model like

public class Status {

    private String label;
    private int status;

    public Status(String label, int status) {
        this.label = label;
        this.status = status;
    }

    public String getLabel() {
        return label;
    }

    public int getStatus() {
        return status;
    }

}

Create a ArryaList of Status

ArrayList<Status> listOfStatus=new ArrayList<Status>();
        listOfStatus.add(new Status("item1", 0));
        listOfStatus.add(new Status("item2", 0));
        listOfStatus.add(new Status("item3", 1));
        listOfStatus.add(new Status("item4", 1));

You need to pass the arrayList in your Adapter class. Now initialize ArrayList in Adapter class say listOfStatus and use it getView() method.

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater lInflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = lInflater.inflate(R.layout.LIST_ITEM_LAYOUT, parent, false);
        }

        if (listOfStatus.get(position).getStatus()==1) {
            view.setBackgroundColor(context.getResources().getColor(R.color.green));
        } else {
            view.setBackgroundColor(context.getResources().getColor(R.color.green));
        }

        return view;
    }
}

Upvotes: 0

Max Kachinkin
Max Kachinkin

Reputation: 186

This sample code can help you to understand basic approach how to work with listview and adapters.

public class MyAdapter extends BaseAdapter
{
    private LayoutInflater m_inflater;
    private MyDataSource m_data;

    public ProfileListAdapter(MyDataSource _data)
    {
        m_inflater = m_activity.getLayoutInflater();
        m_data = _data;
    }

    @Override
    public int getCount()
    {
        return m_data.getCount();
    }

    @Override
    public Object getItem(int arg0)
    {
        return null;
    }

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

    @Override
    public int getViewTypeCount() 
    {
        return 1;
    }

    @Override
    public int getItemViewType(int _position)
    {
        return 0;
    }

    @Override
    public View getView(int _position, View _convertView, ViewGroup _parent)
    {
        View rowView = _convertView;

        if(rowView == null)
        {
            rowView = m_inflater.inflate(R.layout.my_item_view, null);
        }

        if(m_data.m_list.get(_position).status == 0 )
        {
            View my_root_layout = v.findViewById(my_root_layout);
            my_root_layout.setBackgroundResource(R.drawable.my_item_background);
        }   

        fillInTypeHead(rowView);

        return rowView;
    }
}

For more detail information you could go to http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews

And I'll recommend to you watch this tutorial from Google engineers http://www.youtube.com/watch?v=wDBM6wVEO70 It's about basics in ListViews.

Upvotes: 0

TMH
TMH

Reputation: 6246

In your ArrayAdapter you can check the value and change the View's background colour depending on the value.

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157437

It is simple. You need to subclass an Adapter, and override getView(), getViewTypeCount() and getItemViewType()

Upvotes: 1

Related Questions