重复列表视图数据

Duplicate listview data

我正在使用 button 生成 listview,如果在生成 listview 时存在时间间隔,它工作正常,但如果我反复按 button 生成 listview 它将生成重复数据。我怎样才能避免这种情况?

我试过了,

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

在重新创建它们之前。我也试过

notifyDataSetChanged();

适配器class也有,但还是没有解决问题。 我的适配器 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;
    }

}

我正在像这样从 onclick 创建列表视图,

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

你可以这样试试 1. 在你的 ListViewAdapter 添加一个 public 方法

  updateElements (ArrayList<ReportModel> newList) {
    reportElements.clear ();
    reportElements.addAll (newList);
    notifyDataSetChanged ();
    }

然后,在你的 onButtonClick 添加

adapterForReport . updateElements (newList);

AdapterForReport 应该全局声明

reportList是一个字段吗?如果不是,则将其声明为一个,并首先进入 onClick 事件。 reportList.clear(); 最后一步 adapterForReport.notifyDataSetChanged();.

在我的例子中它有效。也许检查这个 Link.

只需在按钮点击时添加以下检查之一。

if(adapterForReport==null)
{
//getData
}//else do nothing //and define adapterForReport golbally. 

or
if(reportList.size()>0 )
{
//do nothing
}//else getData