如何从列表视图中删除来自服务器的重复项目

时间:2015-03-18 07:21:21

标签: android

我对android有点新鲜。这是我的代码。我在这里使用了一个列表,我提取楼层号但是重复了值。

我已将服务器中的重复值存储为json数据

请帮我从列表中删除重复的项目。谢谢所有

   final List<Customer> floors= new ArrayList<Customer>();
   jsonResponse = "";
   for (int i = 0; i < response.length(); i++) {
      JSONObject customer = (JSONObject) response.get(i);
      String email = customer.getString("email");
      String name = customer.getString("f_name");
      Double balance = customer.getDouble("balance");
      String phone = customer.getString("phone");
      String streetName = customer.getString("street");
      String wardName = customer.getString("ward");
      String doorNumber = customer.getString("door");
      String floorNumber = customer.getString("floor");
      String houseType=customer.getString("type");
      floors.add(new Customer(email, name, balance, doorNumber, phone, 
             streetName, houseType,wardName,floorNumber));
   }

从下面我可以获取楼层名称....

public View getView(int position, View convertView, ViewGroup parent) {
  LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View rootView = inflater.inflate(R.layout.activity_floor, parent, false);
  TextView tv = (TextView) rootView.findViewById(R.id.txtResponse2);
  tv.setText(floors.get(position).getFloor());
  return rootView;
}

1 个答案:

答案 0 :(得分:0)

例如,如果要删除电子邮件字段上的重复项,可以使用HashMap。使用电子邮件作为HashMap的密钥,在插入客户之前,测试是否存在电子邮件,如下所示:

final Map<String, Customer> floorsMap= new HashMap<String,Customer>();
   jsonResponse = "";
   for (int i = 0; i < response.length(); i++) {
      JSONObject customer = (JSONObject) response.get(i);
      String email = customer.getString("email");
      String name = customer.getString("f_name");
      Double balance = customer.getDouble("balance");
      String phone = customer.getString("phone");
      String streetName = customer.getString("street");
      String wardName = customer.getString("ward");
      String doorNumber = customer.getString("door");
      String floorNumber = customer.getString("floor");
      String houseType=customer.getString("type");

      if(!floorsMap.containsKey(email)){
          floorsMap.put(email, new Customer(email, name, balance, doorNumber, phone, streetName, houseType,wardName,floorNumber));
      }
   }
   final List<Customer> floors = new ArrayList<Customer>(floorsMap.values());