如何更改listview中每个项目的背景颜色?

时间:2022-02-28 08:11:23

I am new guy for android.

我是android的新人。

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

我从数据库中获取数据并将其显示到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..

也就是说,我从数据库中检索数据,这里有一个字段,比如status。

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

如果状态为1,则项目颜色将变为绿色。

like

就像

如何更改listview中每个项目的背景颜色?

How can i do this.

我该怎么做呢?

It is possible. Please help me.

这是可能的。请帮助我。

Thanks is advance.

由于是进步。

5 个解决方案

#1


1  

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

它是简单的。您需要子类化一个适配器,并覆盖getView()、getViewTypeCount()和getItemViewType()

#2


1  

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

在您的ArrayAdapter中,您可以根据值检查值并更改视图的背景颜色。

#3


1  

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

在您的适配器的getView()方法中,您可以根据数量设置后台资源,如下所示:

// 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与这些内容:

<?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.

注意,如果项目是可点击的,那么在用户单击时提供反馈是很重要的。在这种情况下,您应该使用状态列表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:

接下来,你的list_item。xml布局应该如下所示:

<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>

#4


0  

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

这个示例代码可以帮助您理解如何使用listview和适配器的基本方法。

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

要了解更多的详细信息,可以访问http://developer.android.com/guide/topics/ui/declaration -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.

我将向您推荐谷歌工程师http://www.youtube.com/watch?它是关于ListViews的基础知识。

#5


0  

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.

您需要在适配器类中传递arrayList。现在,在适配器类say listOfStatus中初始化ArrayList,并使用它getView()方法。

@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;
    }
}

#1


1  

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

它是简单的。您需要子类化一个适配器,并覆盖getView()、getViewTypeCount()和getItemViewType()

#2


1  

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

在您的ArrayAdapter中,您可以根据值检查值并更改视图的背景颜色。

#3


1  

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

在您的适配器的getView()方法中,您可以根据数量设置后台资源,如下所示:

// 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与这些内容:

<?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.

注意,如果项目是可点击的,那么在用户单击时提供反馈是很重要的。在这种情况下,您应该使用状态列表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:

接下来,你的list_item。xml布局应该如下所示:

<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>

#4


0  

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

这个示例代码可以帮助您理解如何使用listview和适配器的基本方法。

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

要了解更多的详细信息,可以访问http://developer.android.com/guide/topics/ui/declaration -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.

我将向您推荐谷歌工程师http://www.youtube.com/watch?它是关于ListViews的基础知识。

#5


0  

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.

您需要在适配器类中传递arrayList。现在,在适配器类say listOfStatus中初始化ArrayList,并使用它getView()方法。

@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;
    }
}