androidlistview

Listview showing but double displayed when srcolling


I tried to create a listview of 629 records part from mysql with json, listview running but the data displayed double when in scrollingdown

this is my StockActivity:

public class StockActivity extends Activity {

public ArrayList<HashMap<String, String>> list;
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stock);
    listView=(ListView)findViewById(R.id.listView1);
    list=new ArrayList<HashMap<String,String>>();
    list.clear();
    getPartId();

}
public void getPartId(){
    class GetPartId extends AsyncTask<Void,Void,String> {
        private ProgressDialog pDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    pDialog = new ProgressDialog(StockActivity.this);
                    pDialog.setMessage("Showing Part...wait");
                    pDialog.setIndeterminate(false);
                    pDialog.setCancelable(true);
                    pDialog.show();
                }
            });
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            pDialog.dismiss();
            showPartId(s);
        }

        @Override
        protected String doInBackground(Void... params) {
            String s = null;
            RequestHandler rh = new RequestHandler();
            s = rh.sendGetRequest(konfigurasi.URL_TAMPIL_ALL);
            return s;
        }
    }
    GetPartId gPI = new GetPartId();
    gPI.execute();
}

private void showPartId(String json){

    try {
        //Mencari nilai No_Part

        JSONObject jsonObject = new JSONObject(json);
        JSONArray result = jsonObject.getJSONArray(konfigurasi.TAG_JSON_ARRAY);

        for ( int i=0;i<result.length();i++) {
            JSONObject c = result.getJSONObject(i);
            String No_Part = c.getString(konfigurasi.TAG_NO_PART);
            String Part_Desc = c.getString(konfigurasi.TAG_PART_DESC);
            String Spec1 = c.getString(konfigurasi.TAG_SPEC1);
            String Spec2 = c.getString(konfigurasi.TAG_SPEC2);
            String Sat = c.getString(konfigurasi.TAG_SAT);
            String Location = c.getString(konfigurasi.TAG_LOCATION);
            //String Qty_Akhir = c.getString(konfigurasi.TAG_QTY_AKHIR);

            HashMap<String,String> temp = new HashMap<String, String>();
            temp.put(COLUMN1, No_Part);
            temp.put(COLUMN2, Part_Desc);
            temp.put(COLUMN3, Spec1);
            temp.put(COLUMN4, Spec2);
            temp.put(COLUMN5, Sat);
            temp.put(COLUMN6, Location);
            //temp.put(COLUMN7, Qty_Akhir);

            list.add(temp);
            ListViewAdapter adapter=new ListViewAdapter(this, list);
            adapter.notifyDataSetChanged();listView.setAdapter(adapter);
        }

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

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
        {
            int pos=position+1;
            Toast.makeText(StockActivity.this, Integer.toString(pos)+" Clicked", Toast.LENGTH_SHORT).show();
        }
    });
}

}

This is my Constant

package com.example.asus.mywarehouse;

public class Constants {

    public static final String COLUMN1="No_Part";
    public static final String COLUMN2="Part_Desc";
    public static final String COLUMN3="Spec1";
    public static final String COLUMN4="Spec2";
    public static final String COLUMN5="Sat";
    public static final String COLUMN6="Location";
    public static final String COLUMN7="Qty_Onh";

}

this is my ListViewAdapter

package com.example.asus.mywarehouse;

import static com.example.asus.mywarehouse.Constants.COLUMN1;
import static com.example.asus.mywarehouse.Constants.COLUMN2;
import static com.example.asus.mywarehouse.Constants.COLUMN3;
import static com.example.asus.mywarehouse.Constants.COLUMN4;
import static com.example.asus.mywarehouse.Constants.COLUMN5;
import static com.example.asus.mywarehouse.Constants.COLUMN6;
import static com.example.asus.mywarehouse.Constants.COLUMN7;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter{

    public ArrayList<HashMap<String, String>> list;
    Activity activity;
    TextView txt1;
    TextView txt2;
    TextView txt3;
    TextView txt4;
    TextView txt5;
    TextView txt6;
    TextView txt7;

    public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list){
        super();
        this.activity=activity;
        this.list=list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
        //return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater inflater=activity.getLayoutInflater();

        if(convertView == null){

            convertView=inflater.inflate(R.layout.colm_row, null);

            txt1=(TextView) convertView.findViewById(R.id.No_Part);
            txt2=(TextView) convertView.findViewById(R.id.Part_Desc);
            txt3=(TextView) convertView.findViewById(R.id.Spec1);
            txt4=(TextView) convertView.findViewById(R.id.Spec2);
            txt5=(TextView) convertView.findViewById(R.id.Sat);
            txt6=(TextView) convertView.findViewById(R.id.Location);
            txt7=(TextView) convertView.findViewById(R.id.Qty_Onh);
        }
        HashMap<String, String> map=list.get(position);
        txt1.setText(map.get(COLUMN1));
        txt2.setText(map.get(COLUMN2));
        txt3.setText(map.get(COLUMN3));
        txt4.setText(map.get(COLUMN4));
        txt5.setText(map.get(COLUMN5));
        txt6.setText(map.get(COLUMN6));
        txt7.setText(map.get(COLUMN7));
        convertView.setTag(list.get(position));
        return convertView;
    }
}

can anyone solve this problem, thanks for the help


Solution

  • You need to put this code outside of your for loop.

    ListViewAdapter adapter=new ListViewAdapter(this, list);
    listView.setAdapter(adapter);
    

    And remove this code from inside of the loop

    ListViewAdapter adapter=new ListViewAdapter(this, list);
                adapter.notifyDataSetChanged();listView.setAdapter(adapter);