2017-03-25 79 views
1

我一直在嘗試很多事情來解決這個問題,並尋找解決方案几個小時,我需要一些幫助。我有一個列表視圖,從MySQL導入數據並在Listview中獲取數據。我的問題:列表視圖重複的項目(只有前20,然後我使用「loadmoredata」,我收到滾動downn新20項,但沒有更多的重複項目了)。所以,我第一次看到40個項目(重複20次)。如何使用LinkedHashSet刪除ArrayList <Object>中的重複項?

我試過LinkedHashSet,但我不能得到那個作品。有什麼建議麼?

private ArrayList<Usuarios> items; 
private Set<Usuarios> set; 
private ArrayList<Usuarios> newList; 
Activity activity; 
Usuarios_adapter adapter; 

... 

listView = (ListView) findViewById(R.id.listView); 
    listView.setOnItemClickListener(this); 

    activity=this; 
    items=new ArrayList<Usuarios>(); 
    set = new LinkedHashSet<Usuarios>(items); 
    newList = new ArrayList<Usuarios>(set); 
    adapter=new Usuarios_adapter(activity,newList); 
    listView.setAdapter(adapter); 

    //more code here 

    } 
    class BackTask extends AsyncTask<Void,Void,Void>{ 

    protected void onPreExecute(){ 
     super.onPreExecute(); 
     listView.addFooterView(footerView); 

    } 
    protected Void doInBackground(Void...params) { 
     HashMap<String,String> params2 = new HashMap<>(); 
     params2.put(Config.TAG_PESO,lastpeso2); 
     RequestHandler rh = new RequestHandler(); 
     String s = rh.sendPostRequest(Config.URL_GET_ALL3, params2); 
     JSON_STRING = s; 
     JSONObject jsonObject = null; 
     try { 
      if(isLoadMore){ 
      isLoadMore = false; } 
      jsonObject = new JSONObject(JSON_STRING); 
      JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY); 

      for (int i = 0; i < result.length(); i++) { 
       JSONObject jo = result.getJSONObject(i); 
       String id = String.valueOf(jo.getInt(Config.TAG_ID)); 
       String nombre = jo.getString(Config.TAG_NOMBRE); 
       String email = jo.getString(Config.TAG_EMAIL); 
       String foto = jo.getString(Config.TAG_FOTO); 
       String peso = jo.getString(Config.TAG_PESO); 
       String vasos = jo.getString(Config.TAG_VASOS); 
       String seguimiento = jo.getString(Config.TAG_SEGUIMIENTO); 
       String aplausos = jo.getString(Config.TAG_APLAUSOS); 
       String followers = jo.getString(Config.TAG_FOLLOWERS); 

       Usuarios user = new Usuarios(id, nombre, email, foto, peso, vasos, seguimiento ,aplausos, followers); 
       //items.add(user); //this would be the original, who give me duplicated items 

       newList.add(user);//this is the second arraylist 

      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 


    protected void onPostExecute(Void result){ 
     listView.removeFooterView(footerView); 
     adapter.notifyDataSetChanged(); 




    } 
} 
    BackTask bt = new BackTask(); 
    bt.execute(); 

編輯: 隨着@Japu_D_Cret修飾

private ArrayList<Usuarios> items; 
    private Activity activity; 
    private Usuarios_adapter adapter; 

    ..... 

    activity=this; 
    items=new ArrayList<Usuarios>(); 
    adapter=new Usuarios_adapter(activity,items); 
    listView.setAdapter(adapter); 

    .... 

    Set<Usuarios> set = new HashSet<>(items); 

      for (int i = 0; i < result.length(); i++) { 
       JSONObject jo = result.getJSONObject(i); 
       String id = String.valueOf(jo.getInt(Config.TAG_ID)); 
       String nombre = jo.getString(Config.TAG_NOMBRE); 
       String email = jo.getString(Config.TAG_EMAIL); 
       String foto = jo.getString(Config.TAG_FOTO); 
       String peso = jo.getString(Config.TAG_PESO); 
       String vasos = jo.getString(Config.TAG_VASOS); 
       String seguimiento = jo.getString(Config.TAG_SEGUIMIENTO); 
       String aplausos = jo.getString(Config.TAG_APLAUSOS); 
       String followers = jo.getString(Config.TAG_FOLLOWERS); 

       Usuarios user = new Usuarios(id, nombre, email, foto, peso, vasos, seguimiento ,aplausos, followers); 
       //items.add(user); 
       set.add(user); 


      } 
      //clear the list, so no duplicates occurr 
      items.clear(); 
      //copy the temporary set to the items 
      items.addAll(set); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
     } 
     return null; 
    } 

這是我getView

 public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    if (convertView == null) { 
     LayoutInflater inflator = activity.getLayoutInflater(); 
     convertView = inflator.inflate(R.layout.b_server_list_item, null); 
     holder = new ViewHolder(); 
     holder.id = (TextView) convertView.findViewById(R.id.id); 
     holder.email = (TextView) convertView.findViewById(R.id.list_email); 
     holder.nombre = (TextView) convertView.findViewById(R.id.Nombre); 
     holder.peso = (TextView) convertView.findViewById(R.id.Peso); 
     holder.vasos = (TextView) convertView.findViewById(R.id.Vasos); 
     holder.seguimiento = (TextView) convertView.findViewById(R.id.Seguimiento); 
     holder.followers = (TextView) convertView.findViewById(R.id.tv_followers); 
     holder.aplausos = (TextView) convertView.findViewById(R.id.tv_aplausos); 
     holder.picture = (ImageView) convertView.findViewById(R.id.foto_profile); 
     holder.foto_id = (TextView) convertView.findViewById(R.id.foto_id); 
     holder.rank = (TextView) convertView.findViewById(R.id.Rank); 
     holder.picture = (ImageView) convertView.findViewById(R.id.foto_profile); 
     convertView.setTag(holder); 

    } else { 

     holder = (ViewHolder) convertView.getTag(); 
    } 

    holder.id.setText(items.get(position).getId()); 
    holder.email.setText(items.get(position).getEmail()); 
    holder.nombre.setText(items.get(position).getNombre()); 
    holder.peso.setText(items.get(position).getPeso()); 
    holder.vasos.setText(items.get(position).getVasos()); 
    holder.seguimiento.setText(items.get(position).getSeguimiento()); 
    holder.followers.setText(items.get(position).getFollowers()); 
    holder.aplausos.setText(items.get(position).getAplausos()); 
    holder.foto_id.setText(items.get(position).getFoto()); 
    final int pos = position+1; 
    holder.rank.setText("" + pos); 

    String fotoid = holder.foto_id.getText().toString(); 
    if(fotoid.contains("avatar_a")){holder.picture.setImageResource(R.drawable.avatar_a);} 
    if(fotoid.contains("avatar_b")){holder.picture.setImageResource(R.drawable.avatar_b);} 

    return convertView; 
} 
+0

您不使用Set。您將內容複製到newList,但不實際使用它。如果您的問題得到解答,請參閱我的回答 –

+0

,請標記答案 –

回答

2
在這些線路

,首先要創建一個一個ArrayList的項目,然後副本它內容到Set集合,然後拷貝集合內容在一個新的ArrayList中new名單

items=new ArrayList<Usuarios>(); 
set = new LinkedHashSet<Usuarios>(items); 
newList = new ArrayList<Usuarios>(set); 

我會刪除所有不必要的名單,只與集去,所以你的代碼看起來像這樣

//private ArrayList<Usuarios> items; 
private Set<Usuarios> set; 
//private ArrayList<Usuarios> newList; 

//... 

protected Void doInBackground(Void...params) { 
    HashMap<String,String> params2 = new HashMap<>(); 
    params2.put(Config.TAG_PESO,lastpeso2); 
    RequestHandler rh = new RequestHandler(); 
    String s = rh.sendPostRequest(Config.URL_GET_ALL3, params2); 
    JSON_STRING = s; 
    JSONObject jsonObject = null; 
    try { 
     if(isLoadMore){ 
     isLoadMore = false; } 
     jsonObject = new JSONObject(JSON_STRING); 
     JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY); 

     for (int i = 0; i < result.length(); i++) { 
      JSONObject jo = result.getJSONObject(i); 
      String id = String.valueOf(jo.getInt(Config.TAG_ID)); 
      String nombre = jo.getString(Config.TAG_NOMBRE); 
      String email = jo.getString(Config.TAG_EMAIL); 
      String foto = jo.getString(Config.TAG_FOTO); 
      String peso = jo.getString(Config.TAG_PESO); 
      String vasos = jo.getString(Config.TAG_VASOS); 
      String seguimiento = jo.getString(Config.TAG_SEGUIMIENTO); 
      String aplausos = jo.getString(Config.TAG_APLAUSOS); 
      String followers = jo.getString(Config.TAG_FOLLOWERS); 

      Usuarios user = new Usuarios(id, nombre, email, foto, peso, vasos, seguimiento ,aplausos, followers); 
      //items.add(user); //this would be the original, who give me duplicated items 

      //newList.add(user);//this is the second arraylist 

      set.add(user); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 
您的反饋意見後

我改變它,所以它僅使用一組內功能:

private ArrayList<Usuarios> items; 
//private Set<Usuarios> set; 
//private ArrayList<Usuarios> newList; 

//... 

protected Void doInBackground(Void...params) { 
    HashMap<String,String> params2 = new HashMap<>(); 
    params2.put(Config.TAG_PESO,lastpeso2); 
    RequestHandler rh = new RequestHandler(); 
    String s = rh.sendPostRequest(Config.URL_GET_ALL3, params2); 
    JSON_STRING = s; 
    JSONObject jsonObject = null; 
    try { 
     if(isLoadMore){ 
     isLoadMore = false; } 
     jsonObject = new JSONObject(JSON_STRING); 
     JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY); 

     //make a temporary copy 
     Set<Usuarios> set = new HashSet<>(items); 

     for (int i = 0; i < result.length(); i++) { 
      JSONObject jo = result.getJSONObject(i); 
      String id = String.valueOf(jo.getInt(Config.TAG_ID)); 
      String nombre = jo.getString(Config.TAG_NOMBRE); 
      String email = jo.getString(Config.TAG_EMAIL); 
      String foto = jo.getString(Config.TAG_FOTO); 
      String peso = jo.getString(Config.TAG_PESO); 
      String vasos = jo.getString(Config.TAG_VASOS); 
      String seguimiento = jo.getString(Config.TAG_SEGUIMIENTO); 
      String aplausos = jo.getString(Config.TAG_APLAUSOS); 
      String followers = jo.getString(Config.TAG_FOLLOWERS); 

      Usuarios user = new Usuarios(id, nombre, email, foto, peso, vasos, seguimiento ,aplausos, followers); 
      //items.add(user); //this would be the original, who give me duplicated items 

      //newList.add(user);//this is the second arraylist 

      set.add(user); 
     } 

     //clear the list, so no duplicates occurr 
     list.clear(); 
     //copy the temporary set to the items 
     list.addAll(set); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 
+0

我試過了。如果我這樣做,我收到0件物品並崩潰。我至少需要'私人ArrayList的 items'因爲我在適配器使用它'適配器=新Usuarios_adapter(活動項目);'我刪除了innecesary第二數組列表,但仍doesn't工作 – SanHappy

+0

@SanHappy我編輯它 –

+0

壞運氣再次。現在再次收到40件物品。我試着用'HashSet'像你說的我,然後用'LinkedHashSet',因爲我需要尊重順序,但兩者給我的40項(20重複)。 – SanHappy