【问题标题】:How to do custom ListView with colorful items' backgrounds?如何使用彩色项目的背景自定义 ListView?
【发布时间】:2010-11-16 22:40:09
【问题描述】:

我创建了一个ArrayList<HashMap<String, String>> 集合来保存ListView 的数据。我正在使用SimpleAdapter

当列表项的 ID % 10 == 0 时,是否可以更改列表项的背景?

这里是代码(方法生成布局):

private void fillData() {

    Cursor c = this.mDbManager.getNgOrderDetailByOrderNumber(this.mNumber);

    ArrayList<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>();

    if (!c.isAfterLast()) {
       do {
           // ... filling HashMap and putting it to ArrayList
       } while (c.moveToNext());   
    }

    SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.list_item, 
        new String[] { "product", "ordered", "price", "discount" }, 
        new int[] { R.id.ProductTextView, R.id.OrderedTextView,
        R.id.PriceTextView, R.id.DiscountTextView });
     ListView l = (ListView) findViewById(android.R.id.list);
     l.setAdapter(adapter);
}

【问题讨论】:

    标签: android android-listview listviewitem android-sdk-2.1


    【解决方案1】:

    您在适配器中覆盖 getView 以更改视图。请记住,ListView 会重复使用视图实现,因此如果将颜色更改为第 10 项,请确保将所有其他视图的颜色设置为相反。

    例如

    new SimpleAdapter( ... ) {
      @Override
      public View getView (int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        if (position == 10) {
          // set background color = red;
        } else {
          // set background color = green;
        }
        return view;
      }
    }
    

    【讨论】:

      【解决方案2】:

      这是代码,希望对其他用户有所帮助

      private void fillData() {
      
          Cursor c = this.mDbManager.getNgOrderDetailByOrderNumber(this.mNumber);
      
          ArrayList < HashMap < String, String >> items = new ArrayList < HashMap < String, String >> ();
      
          if (!c.isAfterLast()) {
              do {
                  // ... filling HashMap and putting it to ArrayList
              } while (c.moveToNext());
          }
      
          SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.list_item,
              new String[] {
              "product", "ordered", "price", "discount"
          },
              new int[] {
              R.id.ProductTextView, R.id.OrderedTextView,
              R.id.PriceTextView, R.id.DiscountTextView
          }) {
      
              // here is the method you need to override, to achieve colorful list
      
              @Override
              public View getView(int position, View convertView, ViewGroup parent) {
      
                  View view = super.getView(position, convertView, parent);
      
                  HashMap < String, String > items = (HashMap < String, String > ) getListView()
                      .getItemAtPosition(position);
                  if (Long.parseLong(items.get("id")) % 10 == 0) {
                      view.setBackgroundColor(Color.GREEN);
                  } else {
                      view.setBackgroundColor(Color.YELLOW);
                  }
                  return view;
              }
      
          };
          ListView l = (ListView) findViewById(android.R.id.list);
          l.setAdapter(adapter);
      }
      

      【讨论】:

        【解决方案3】:

        为此,您需要创建一个自定义数组适配器,然后在条件合适的情况下更改背景颜色。

        查看此帖子以获取示例:Custom ArrayAdapter setBackground in getView

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-02-12
          • 2010-12-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多