【问题标题】:changing the colour of a listview item更改列表视图项的颜色
【发布时间】:2020-01-22 12:09:03
【问题描述】:

我有一个列表视图,其中包含有关我的客户的信息。它基本上是已预订工作的用户列表。 listview 使用 firebase 数据库作为其来源。它连接到一个名为 Bookings 的节点。然后它将用户 ID 作为其密钥。

{
  "Bookings": {
    "User-Id appears here": {
      "Fullname": "Name Appears here",
      "Date": "",
      "Valet Type": "",
      "Car Model": "",
      "CustomerID": "",
      "Approved": ""
    }
  }
}

已批准节点可以是“未批准”、“已批准”、“已取消”三个值之一 根据 Approved 值,我想更改包含数据的列表视图项的颜色。我已经设法让列表视图在单击项目时更改颜色,但我希望它保持该颜色,除非“已批准”节点另有说明。

这是我目前拥有的代码,当点击项目时它会发生变化。

bookingsref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
            for (final DataSnapshot snapshot : dataSnapshot.getChildren()) {

                date = snapshot.child("Date").getValue().toString();
                name = snapshot.child("Fullname").getValue().toString();
                valet = snapshot.child("ValetType").getValue().toString();
                model = snapshot.child("VehicleModel").getValue().toString();
                customerid = snapshot.child("UserId").getValue().toString();

                confirm = snapshot.child("Approved").getValue().toString();
                ApproveArray.add(confirm);
                bookingID.add(customerid);

booking_info_minor.add(date + "\n" + "Name: "+ name + "\n"+"Valet Type: "+valet + "\n" +"Model: " + model + "\n"+"Customer ID: "+ customerid );

                adapter =new ArrayAdapter<String>(Bookings .this,android.R.layout.simple_list_item_1,booking_info_minor);
                lv.setAdapter(adapter);

                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String c = bookingID.get(position).toString();
                        confirm = ApproveArray.get(position);


                        Intent intent=new Intent(getBaseContext(),Booking_details.class);
                        Bundle bundle = new Bundle();
                        bundle.putString( "UserId",c);
                        intent.putExtras(bundle);
                        startActivity(intent);
                        listColour(position,confirm);


                    }
                });

            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

private void listColour(int pos,String confirm){
    Log.i("Conf",confirm);
    if (confirm.equals("Approved")){
        lv = findViewById(R.id.templist);
        lv.getChildAt(pos).setBackgroundResource(R.color.approved);
    }
    if (confirm.equals("Not Approved")) {
        lv = findViewById(R.id.templist);
        lv.getChildAt(pos).setBackgroundResource(R.color.not_approved);
    }

    if (confirm.equals("Canceled")) {
        lv = findViewById(R.id.templist);
        lv.getChildAt(pos).setBackgroundResource(R.color.canceled);
    }

【问题讨论】:

    标签: java android firebase listview firebase-realtime-database


    【解决方案1】:

    只需Override ArrayAdapter 的getView 并进行如下适当更改:

    adapter = new ArrayAdapter<String>(Bookings.this, android.R.layout.simple_list_item_1, booking_info_minor) {
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    
            View view = super.getView(position, convertView, parent);
    
            String confirm = ApproveArray.get(position);
    
            switch (confirm) {
                case "Approved":
                    view.setBackgroundResource(R.color.approved);
                    break;
                case "Not Approved":
                    view.setBackgroundResource(R.color.not_approved);
                    break;
                case "Canceled":
                    view.setBackgroundResource(R.color.canceled);
                    break;
            }
    
            return view;
        }
    };
    

    【讨论】:

    • 我使用了两个答案的组合。我使用了一个适配器,然后在 getview 中添加了您的代码,谢谢
    【解决方案2】:

    只需在您的适配器getView() 方法中,调用类似listColour(position,confirm); 的函数,您可以在其中为列表项视图设置文本。如需更多详细信息,请添加您的列表适配器代码。

    【讨论】:

    • 我的问题是我刚刚为我的列表视图使用了基本适配器吗?
    • adapter =new ArrayAdapter(Bookings .this,android.R.layout.simple_list_item_1,booking_info_minor); lv.setAdapter(适配器);
    • Oo.. 然后使用自定义适配器。
    【解决方案3】:

    我最终使用了上述答案的组合,我最终为我的列表制作了一个基本适配器,然后添加了一个开关以允许在 get 视图中选择已批准的节点。

    在我的预订活动中,我不得不在 onDataChange 方法中添加更多数组

      Name_Array.add(name);
                    Date_Array.add(date);
                    Valet_Array.add(valet);
                    Model_Array.add(model);
                    CustomerId.add(customerid);
                    ApproveArray.add(confirm);
    
                    final String[] name = Name_Array.toArray(new String[Name_Array.size()]);
                    final String[] approve = ApproveArray.toArray(new String[ApproveArray.size()]);
                    String[] date = Date_Array.toArray(new String[Date_Array.size()]);
                    String[] valet = Valet_Array.toArray(new String[Valet_Array.size()]);
                    final String[] model = Model_Array.toArray(new String[Model_Array.size()]);
                    final String[] customerid = CustomerId.toArray(new String[CustomerId.size()]);
    
                    ArrayAdapter apkData = new ArrayAdapter(getBaseContext(), name, valet, model, customerid, date, approve);
    

    然后我的基础 adpater 看起来像这样

    public class ArrayAdapter extends BaseAdapter{
    
        String[] Name, ValetType, Model;
        String[] CustomerId,Date,Approved;
        Context mContext;
        ImageView report;
        TextView tvname,tvdate,tvvalet,tvmodel,tvid,tvapproved;
    
        public ArrayAdapter() {
            Name = null;
            ValetType = null;
            Model = null;
            Date = null;
            CustomerId = null;
            Approved = null;
        }
    
        public ArrayAdapter(Context c, String[] name, String[] valettype, String[] model,String[]customerid,String[] date,String[] approved) {
            Name = name;
            ValetType = valettype;
            Model = model;
            this.mContext = c;
            Date = date;
            CustomerId =customerid;
            Approved = approved;
        }
    
        public int getCount() {
            // TODO Auto-generated method stub
            return Name.length;
        }
    
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }
    
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        public View getView(final int position, View convertView, ViewGroup parent) {
            final View row;
            if (convertView == null) {
                row = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
                }
             else {
                row = convertView;
            }
    
            tvname =  row.findViewById(R.id.tv_name);
            tvdate =  row.findViewById(R.id.tvdate);
            tvvalet =  row.findViewById(R.id.tv_valet);
            tvapproved = row.findViewById(R.id.tvconf);
            tvmodel =  row.findViewById(R.id.tv_model);
            tvid =  row.findViewById(R.id.tvid);
    
            tvdate.setText(Date[position]);
            tvname.setText(Name[position]);
            tvvalet.setText(ValetType[position]);
            tvapproved.setText(Approved[position]);
            tvmodel.setText(Model[position]);
            tvid.setText(CustomerId[position]);
            String approve =Approved[position];
    
            switch (approve) {
                case "Approved":
                    row.setBackgroundResource(R.color.approved);
                    break;
                case "Not Approved":
                    row.setBackgroundResource(R.color.not_approved);
                    break;
                case "Canceled":
                    row.setBackgroundResource(R.color.canceled);
                    break;
    
            }
            return (row);
        }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-28
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      • 2022-07-19
      相关资源
      最近更新 更多