如何根据Android中ListView中每个项目的条件添加颜色?

时间:2016-08-26 08:36:45

标签: android listview

当我尝试动态显示ListView中的每个项目的颜色时,我遇到了奇怪的问题。
问题是,我在列表中有10个项目,我应该将背景颜色应用于项目基于paratemer传递给适配器。请在下面找到代码

public class LazyAdapterForDelivery extends BaseAdapter {
   private ArrayList<HashMap<String, String>> data = new ArrayList<>();
   private static LayoutInflater inflater=null;

   public LazyAdapterForDelivery(Activity a, ArrayList<HashMap<String,    String>> d) {

       data=d;
       System.out.println("The Size of list in Lazy Adapter is: "+d.size());
       inflater = (LayoutInflater)a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   }
   public LazyAdapterForDelivery(Activity a) {
       inflater = (LayoutInflater)a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   }
   public void addItem(HashMap<String, String> item) {
       data.add(item);
       notifyDataSetChanged();
       System.err.println("the size of the adapter is in addItem: " + getCount());
   }
   public int getCount() {
       return data.size();
   }
   public void itemsAdded() {
       for(HashMap<String, String> map : data) {
           System.err.println("Items: "+map);
       }
   }

   public HashMap<String, String> getItem(int position) {
       return data.get(position);
   }

   public long getItemId(int position) {
       return position;
   }

   public View getView(int position, View convertView, ViewGroup parent) {
       System.err.println("Inside GET VIEW");
       View vi=convertView;
       if(convertView==null)
           vi = inflater.inflate(R.layout.aa_pod_lazy_adapter_delivery, null);

       TextView customerName = (TextView)vi.findViewById(R.id.podDeliveryAdapterName); // Customer Name
       TextView trackingId = (TextView)vi.findViewById(R.id.podDeliveryAdapterTrackingId); // Tracking Id
       TextView orderDetails = (TextView)vi.findViewById(R.id.podDeliveryAdapterDetails); // duration
       RelativeLayout **colorLayout** = (RelativeLayout)vi.findViewById(R.id.colorLayout);   //backgroud color

       HashMap<String, String> map = new HashMap<String, String>();
       map = data.get(position);

       DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
       Date dbAssignedDate = new Date();
       String today =  dateFormat.format(dbAssignedDate);

       // Setting all values in listview
       customerName.setText(map.get(DeliveryListFragmentTab.CUSTOMER_NAME));
       trackingId.setText(map.get(DeliveryListFragmentTab.TRACKING_ID));
       orderDetails.setText(map.get(DeliveryListFragmentTab.ORDER_DETAILS));
       optionalMarking.setText(map.get(DeliveryListFragmentTab.OPTIONAL_MARKING));


       if(map.get(DeliveryListFragmentTab.COLOR_IDENTIFICATION)!= null && map.get(DeliveryListFragmentTab.COLOR_IDENTIFICATION).trim().length() > 0 && map.get(DeliveryListFragmentTab.COLOR_IDENTIFICATION).trim().equalsIgnoreCase("green")) {
          System.err.println("Inside green ifff");
          colorLayout.setBackgroundColor(Color.parseColor("#6FA6FF"));
       } else if(map.get(DeliveryListFragmentTab.COLOR_IDENTIFICATION).trim().equalsIgnoreCase("yellow") ) {
           System.err.println("Inside yellow ifff and TrackingId = " + map.get(DeliveryListFragmentTab.TRACKING_ID));
           colorLayout.setBackgroundColor(Color.parseColor("#FFB960"));
       }

       return vi;
   }
}

但是,在10个项目中,只有项目我根据适配器的参数将背景颜色设置为黄色。虽然,它正在回顾列表中的一些项目。当我向上和向下滚动几次时,ListView中的所有项目都是黄色的背景。如果我错了,请帮助我。

Click Here对于我的列表视图的图像,其中“VENKATESH GANGUMOLU”应该只有它的背景颜色。但是“Divya”也拥有它应该没有的东西。

3 个答案:

答案 0 :(得分:0)

因为你的代码中没有其他条件的颜色只是添加一个默认颜色的else条件。

String defaultColor = "#ffffff"; // some default color or white color

if(map.get(DeliveryListFragmentTab.COLOR_IDENTIFICATION)!= null && map.get(DeliveryListFragmentTab.COLOR_IDENTIFICATION).trim().length() > 0 && map.get(DeliveryListFragmentTab.COLOR_IDENTIFICATION).trim().equalsIgnoreCase("green")) {
          System.err.println("Inside green ifff");
          colorLayout.setBackgroundColor(Color.parseColor("#6FA6FF"));
       } else if(map.get(DeliveryListFragmentTab.COLOR_IDENTIFICATION).trim().equalsIgnoreCase("yellow") ) {
           System.err.println("Inside yellow ifff and TrackingId = " + map.get(DeliveryListFragmentTab.TRACKING_ID));
           colorLayout.setBackgroundColor(Color.parseColor("#FFB960"));
       } else {
           colorLayout.setBackgroundColor(Color.parseColor(defaultColor));
       }

答案 1 :(得分:0)

您需要使用ViewHolder模式来实施custom listview adapter

首先在您的<{p>>中创建ViewHolder class

private static class ViewHolder {

   public TextView customerName;
   public TextView trackingId;
   public TextView orderDetails;
   public RelativeLayout colorLayout;
}

然后在adapter's getView method中使用它,如下所示

ViewHolder holder;
if (convertView == null) {
   convertView = inflater.inflate(R.layout.aa_pod_lazy_adapter_delivery, null);
   holder.customerName = (TextView)vi.findViewById(R.id.podDeliveryAdapterName); // Customer Name
   holder.trackingId = (TextView)vi.findViewById(R.id.podDeliveryAdapterTrackingId); // Tracking Id
   hodler.orderDetails = (TextView)vi.findViewById(R.id.podDeliveryAdapterDetails); // duration
   holder.colorLayout= (RelativeLayout)vi.findViewById(R.id.colorLayout);   //backgroud color
   convertView.setTag(holder);
}
// view is recycling
else {
   holder = (ViewHolder) convertView.getTag();
}

并使用所有引用,如

customerName.setTextholder.customerName.setText

答案 2 :(得分:0)

您遇到此问题是因为您忘记在文件中设置默认颜色,因此请在代码中添加一行。试试这个

 if(map.get(DeliveryListFragmentTab.COLOR_IDENTIFICATION)!= null && map.get(DeliveryListFragmentTab.COLOR_IDENTIFICATION).trim().length() > 0 && map.get(DeliveryListFragmentTab.COLOR_IDENTIFICATION).trim().equalsIgnoreCase("green")) {
      System.err.println("Inside green ifff");
      colorLayout.setBackgroundColor(Color.parseColor("#6FA6FF"));
   } else if(map.get(DeliveryListFragmentTab.COLOR_IDENTIFICATION).trim().equalsIgnoreCase("yellow") ) {
       System.err.println("Inside yellow ifff and TrackingId = " + map.get(DeliveryListFragmentTab.TRACKING_ID));
       colorLayout.setBackgroundColor(Color.parseColor("#FFB960"));
   }else{
      // ---------add this line -----------------
    colorLayout.setBackgroundColor(Color.parseColor("#FFFFFF")); // white
}