重复的ListView数据
问题描述:
我使用的是button
产生listview
,它工作正常,如果在产生listview
一个timegap,但如果我重复按button
产生listview
将与重复数据产生。我怎么能避免这种情况?重复的ListView数据
我都试过了,
nameList.clear();
selfAmountList.clear();
officeAmountList.clear();
totalAmountList.clear();
reportList.setAdapter(null);
再重新创建它们之前。我也试过
notifyDataSetChanged();
在适配器类也,但仍然没有解决问题。 我的适配器类:
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);
答
只需在按钮单击中添加以下检查之一。
if(adapterForReport==null)
{
//getData
}//else do nothing //and define adapterForReport golbally.
or
if(reportList.size()>0)
{
//do nothing
}//else getData
答
你可以尝试这样的 1.在ListViewAdapter添加一个公共方法
updateElements (ArrayList<ReportModel> newList) {
reportElements.clear();
reportElements.addAll (newList);
notifyDataSetChanged();
}
然后,在onButtonClick上添加
adapterForReport . updateElements (newList);
AdapterForReport应该全局声明
答
reportList是一个字段吗?如果不是,请将其声明为一个,然后首先进入onClick事件。 reportList.clear();
,最后一步为adapterForReport.notifyDataSetChanged();
。
在我的情况下,它的工作原理。也许检查Link。
发布您的信息。 –
我已经在问题中添加了我的适配器类。 – minion
首先不要调用AdapterForReport.this.notifyDataSetChanged();从你的getView。同时请发布您的按钮onclicklistener的代码。我认为问题在于此。 –