我正在使用listview
生成button
,如果在生成listview
时存在时间差,则工作正常,但如果我反复按button
生成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这样的listview,
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);
答案 0 :(得分:0)
你可以这样试试 1.在ListViewAdapter中添加一个公共方法
updateElements (ArrayList<ReportModel> newList) {
reportElements.clear ();
reportElements.addAll (newList);
notifyDataSetChanged ();
}
然后,在你的onButtonClick添加
adapterForReport . updateElements (newList);
应该全局声明AdapterForReport
答案 1 :(得分:0)
reportList是一个字段吗?如果没有,请将其声明为一个并在第一步进入onClick事件。 reportList.clear();
以及最后一步adapterForReport.notifyDataSetChanged();
。
就我而言,它有效。也许请查看Link。
答案 2 :(得分:0)
只需在按钮点击时添加以下检查之一即可。
if(adapterForReport==null)
{
//getData
}//else do nothing //and define adapterForReport golbally.
or
if(reportList.size()>0 )
{
//do nothing
}//else getData