如何使用彩色项目自定义 ListView?背景?

发布于 2024-10-03 00:59:50 字数 952 浏览 8 评论 0 原文

我创建了一个 ArrayList> 集合来保存 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);
}

I have created an ArrayList<HashMap<String, String>> collection to hold my data for ListView. I'm using SimpleAdapter.

Is it possible to change background of list item when list item's ID % 10 == 0?

Here is the code (method generating layout):

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尛丟丟 2024-10-10 00:59:50

您可以在适配器中重写 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;
  }
}

You override getView in your adapter to make changes to the view. Keep in mind that ListView reuses the view implementations, so if you change the color to item 10, make sure you set the color to the opposite for all other views.

e.g.

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;
  }
}
剩一世无双 2024-10-10 00:59:50

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

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

Here is the code, hope it'll be helpful for other users

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);
}
枯寂 2024-10-10 00:59:50

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

查看这篇文章的示例:自定义 ArrayAdapter setBackground in getView

To accomplish this, you need to create a custom array adapter and then change the background color if the conditions are right.

Check out this post for an example: Custom ArrayAdapter setBackground in getView

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文