1

I have been trying many things to solve this problem and searching for the solution for hours, I need some help with this. I have a listview, importing data from MySQL and fetching data in Listview. My problem: the listview duplicate items (Only the first 20, then I use "loadmoredata" and I receive on scroll downn new 20 items, but no more duplicated items again). So, the first time I see 40 items (20 repeated).

I have tried LinkedHashSet but I cant get that works. Any suggestions?

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();

EDIT: With @Japu_D_Cret modification

    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;
    }

And this is my 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;
}
2
  • you do not use the Set. you copy the contents to newList, but don't actually use it. see my answer below Commented Mar 25, 2017 at 18:18
  • if your question is answered, please mark the answer Commented Mar 25, 2017 at 18:25

1 Answer 1

2

in these lines, you first create a an ArrayList items, then copy it's content to a Set set, then copy the Sets content in a new ArrayList newlist

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

i would remove all unnecessary lists and only go with the Set, so your code will look like this

//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;
}

after your feedback i altered it, so it only uses a Set inside the function:

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;
}
Sign up to request clarification or add additional context in comments.

7 Comments

I tried. If I do that I receive 0 items and crash. I need at least private ArrayList<Usuarios> items because I have to use it in the adapter adapter=new Usuarios_adapter(activity,items); I deleted the innecesary second arraylist but still doesn´t work
Bad lucky again. Now a receive 40 items again. I tried with HashSet like you said me and then with LinkedHashSet because I need the respect the order but both give me 40 item (20 repeated).
@SanHappy can you edit your question and add how your code looks now? maybe jsut a small typo now :)
I inserted my getView
@SanHappy ok, a Set can only determine if an object is a duplicate if you override the equals method(so your Usuarios class should override it and check with the primary key(i.e. ID)), see this for reference stackoverflow.com/questions/16462644/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.