【问题标题】:Make same background color for same items in ListView为 ListView 中的相同项目设置相同的背景颜色
【发布时间】:2016-08-10 00:39:44
【问题描述】:

我有一个ListView,我想比较我的项目。 如果项目相同,我想为ListView 中的那些项目制作相同的背景颜色。 你能帮帮我吗?

【问题讨论】:

  • 我的列表视图中有一个人员列表(姓名和年龄)。我在比较这些人的年龄。如果两个或更多人的年龄相同,我想在这些人的列表视图中放置相同的颜色。
  • AlertDialog alertDialogObject = builder.create(); ListView listView=alertDialogObject.getListView(); listView.setDivider(new ColorDrawable(Color.BLUE));生产者 p1 = null, p2 = null; if(p1.getTour().equals(p2.getTour())){ listView.setBackgroundColor(Color.BLUE); }

标签: android listview background-color


【解决方案1】:

您必须为您的列表创建一个适配器。 在适配器内部,您可以执行您的操作,例如“自定义”单行布局

【讨论】:

    【解决方案2】:

    您可以通过 ListView 的自定义适配器来执行此操作。

    在以下适配器中(以 SQL Lite 数据库查询作为源数据的 ListView 的光标适配器)在 getView 方法中将背景设置为交替颜色。检测相同项目的代码可能会更复杂,并且取决于数据。

    /**
     * Created by Mike092015 on 17/02/2016.
     */
    public class Database_Inspector_ProductsDB_Adadpter extends CursorAdapter {
        public Database_Inspector_ProductsDB_Adadpter(Context context, Cursor cursor, int flags) {
            super(context, cursor, 0);
        }
    
        @Override
        public View getView(int position, View convertview, ViewGroup parent) {
            View view = super.getView(position, convertview, parent);
            Context context = view.getContext();
            if (position % 2 == 0) {
                view.setBackgroundColor(ContextCompat.getColor(context, R.color.colorlistviewroweven));
            } else {
                view.setBackgroundColor(ContextCompat.getColor(context, R.color.colorlistviewrowodd));
            }
            return view;
        }
    
    
        @Override
        public void bindView(View view, Context context, Cursor cursor) {
    
            TextView textviewproductid = (TextView) view.findViewById(R.id.adipe_productsdb_id);
            TextView textviewproductname = (TextView) view.findViewById(R.id.adipe_productsdb_name);
            TextView textviewproductorder = (TextView) view.findViewById(R.id.adipe_productsdb_order);
            TextView textviewproductaisle = (TextView) view.findViewById(R.id.adipe_productsdb_aisle);
            TextView textviewproductuses = (TextView) view.findViewById(R.id.adipe_productsdb_uses);
            TextView textviewproductnotes = (TextView) view.findViewById(R.id.adipe_productsdb_notes);
    
            textviewproductid.setText(cursor.getString(ShopperDBHelper.PRODUCTS_COLUMN_ID_INDEX));
            textviewproductname.setText(cursor.getString(ShopperDBHelper.PRODUCTS_COLUMN_NAME_INDEX));
            textviewproductorder.setText(cursor.getString(ShopperDBHelper.PRODUCTS_COLUMN_ORDER_INDEX));
            textviewproductaisle.setText(cursor.getString(ShopperDBHelper.PRODUCTS_COLUMN_AISLE_INDEX));
            textviewproductuses.setText(cursor.getString(ShopperDBHelper.PRODUCTS_COLUMN_USES_INDEX));
            textviewproductnotes.setText(cursor.getString(ShopperDBHelper.PRODUCTS_COLUMN_NOTES_INDEX));
    
        };
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            return LayoutInflater.from(context).inflate(R.layout.activity_database_inspect_productsdb_entry,parent, false);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-07-09
      • 1970-01-01
      • 1970-01-01
      • 2012-02-23
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 2019-05-19
      • 1970-01-01
      相关资源
      最近更新 更多