【问题标题】:How can i change the background color of each items in listview?如何更改列表视图中每个项目的背景颜色?
【发布时间】:2013-10-24 10:51:29
【问题描述】:

我是安卓新手。

我完成了从数据库中获取数据并将其显示到列表视图..

现在我想为每个项目设置背景颜色。

那是我从数据库中检索数据,这里有一个字段,比如状态..

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

喜欢

我该怎么做。

这是可能的。请帮帮我。

先谢谢了。

【问题讨论】:

    标签: android database listview android-listview


    【解决方案1】:

    这很简单。您需要继承一个 Adapter,并覆盖 getView()getViewTypeCount()getItemViewType()

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        在你的adapter的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));
        }
        

        现在您需要通过创建包含以下内容的文件 colors.xml 来确保在资源中定义这些颜色:

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

        请注意,如果项目是可点击的,请务必在用户点击时向其提供反馈。在这种情况下,您应该使用state list drawable


        评论后编辑。假设 Item 包含名称和状态,您的适配器应该如下所示。

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

        接下来,您的 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>
        

        【讨论】:

        • 我正在使用这个 ArrayAdapter 它只显示 list_name 并且只显示一个列...我如何显示带有两个列的列表视图.. ArrayAdapter adapter = new ArrayAdapter(this, android .R.layout.simple_list_item_1, db.getAll_CheckList_Name());// 资源) Toast.makeText(getApplicationContext(), db.getAll_CheckList_Name().toString(), 10).show(); l1.setAdapter(适配器);
        • 我用一个适配器示例更新了我的答案,您可以在其中使用自定义视图,以及该视图在 xml 中的外观。
        • 这部分抛出错误 holder.name.setText(getItem(position).name); holder.status.setText(getItem(position).status);
        • 我忘记在 if 子句中将 ViewHolder 设置为 convertView 的标签。 (convertView.setTag(holder);)。我更新了答案。
        • 我无法删除这 2 个错误 holder.name.setText(getItem(position).name); holder.status.setText(getItem(position).status);
        【解决方案4】:

        此示例代码可以帮助您了解如何使用列表视图和适配器的基本方法。

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

        有关更多详细信息,您可以访问 http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews

        我会推荐您观看 Google 工程师 http://www.youtube.com/watch?v=wDBM6wVEO70 的本教程 这是关于 ListViews 的基础知识。

        【讨论】:

          【解决方案5】:

          创建一个类似的模型

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

          创建状态的 ArryaList

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

          您需要在 Adapter 类中传递 arrayList。现在在 Adapter 类中初始化 ArrayList 说 listOfStatus 并使用它的 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;
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-04-14
            • 1970-01-01
            • 2016-01-15
            相关资源
            最近更新 更多