Home > Blockchain >  Duplicate values listView - Android
Duplicate values listView - Android

Time:10-20

I'm developing an Android App with Android Studio, and I have the next problem: this is my Screen without search anything.

Screen

The first time I search something works fine, but when I search again it duplicates my values.

First Search

Second Search - duplicate results

Here is my code when I call to my Host and using my adapter:

cliente.post(URL, new AsyncHttpResponseHandler() {


    @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
        if (statusCode == 200) {


            try {
                JSONArray jsonArray = new JSONArray(new String(responseBody));
                for (int i = 0; i < jsonArray.length(); i  ) {
                    nombreRepuestoList.add(jsonArray.getJSONObject(i).getString("DESCRIPCION"));
                    referenciaRepuestoList.add(jsonArray.getJSONObject(i).getString("REFERENCIA_REPUESTO"));
                }


                    ArrayAdapter adaptermaquina = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_2, android.R.id.text1, nombreMaquinaList) {
                        @Override
                        public View getView(int position, View convertView, ViewGroup parent) {
                            View view = super.getView(position, convertView, parent);
                            TextView text1 = (TextView) view.findViewById(android.R.id.text1);
                            TextView text2 = (TextView) view.findViewById(android.R.id.text2);

                            text1.setText(nombreMaquinaList.get(position));
                            text2.setText(referenciaMaquinaList.get(position));
                            return view;
                        }
                    };
                    adaptermaquina.notifyDataSetChanged();
                    listaResultado.setAdapter(adaptermaquina);

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

I need some help please.

I tried using an empty adapter but it doesn't work and using listaResultado.clear() neither works.

CodePudding user response:

Don't need to clear adatapter simply clear list values

 nombreRepuestoList.clear();
 referenciaRepuestoList.clear(); 

in yout code

    if (statusCode == 200) {
     nombreRepuestoList.clear();//1st 
     referenciaRepuestoList.clear();//2nd 
        try {
            JSONArray jsonArray = new JSONArray(new String(responseBody));
            for (int i = 0; i < jsonArray.length(); i  ) {
                nombreRepuestoList.add(jsonArray.getJSONObject(i).getString("DESCRIPCION"));
                referenciaRepuestoList.add(jsonArray.getJSONObject(i).getString("REFERENCIA_REPUESTO"));
            } 

I hope it works for you

  • Related