Search code examples
androidlistviewcustom-adapter

Duplicate listview data


I am generating a listview using a button , and it is working fine if there is a timegap in generating the listview, but if i am repeatedly pressing the button to generate listview it would generate with duplicate data. How could i avoid this ?

I have tried ,

    nameList.clear();
    selfAmountList.clear();
    officeAmountList.clear();
    totalAmountList.clear();
    reportList.setAdapter(null);

before recreating them again. I have also tried

notifyDataSetChanged();

in adapter class also, but still not solving the problem. my adapter class:

   package com.nerdcastle.nazmul.mealdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by Nazmul on 2/22/2016.
 */
public class AdapterForReport extends ArrayAdapter<ReportModel> {
    private ArrayList<ReportModel> reportList;
    private Context context;

    public AdapterForReport(ArrayList<ReportModel> reportList, Context context) {
        super(context, R.layout.custom_row_for_report,reportList);
        this.reportList=reportList;
        this.context=context;
    }

    private static class ViewHolder {
        public TextView nameTV;
        public TextView selfAmountTV;
        public TextView officeAmountTV;
        public TextView totalTV;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.custom_row_for_report,null);
            holder = new ViewHolder();
            holder.nameTV = (TextView) view.findViewById(R.id.nameTV);
            holder.selfAmountTV = (TextView) view.findViewById(R.id.selfAmountTV);
            holder.officeAmountTV = (TextView) view.findViewById(R.id.officeAmountTV);
            holder.totalTV = (TextView) view.findViewById(R.id.totalTV);
            view.setTag(holder);

        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.nameTV.setText(reportList.get(position).getName());
        holder.selfAmountTV.setText(reportList.get(position).getSelfAmount());
        holder.totalTV.setText(reportList.get(position).getTotal());
        holder.officeAmountTV.setText(reportList.get(position).getOfficeAmount());
        return view;
    }

}

I am creating listview from onclick like this,

for (int i = 0; i < response.length(); i++) {
                try {
                    String name = response.getJSONObject(i).getString("EmployeeName");
                    String total = response.getJSONObject(i).getString("TotalAmount");
                    String selfAmount = response.getJSONObject(i).getString("SelfAmount");
                    String officeAmount = response.getJSONObject(i).getString("OfficeAmount");
                    reportModel=new ReportModel(name,total,selfAmount,officeAmount);
                    reportList.add(reportModel);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            AdapterForReport adapterForReport = new AdapterForReport( reportList,getBaseContext());
            reportListView.setAdapter(adapterForReport);

Solution

  • Just add one of following checks on button click.

    if(adapterForReport==null)
    {
    //getData
    }//else do nothing //and define adapterForReport golbally. 
    
    or
    if(reportList.size()>0 )
    {
    //do nothing
    }//else getData