我多次使用自定义列表视图但我没有遇到过这个问题。我看到很多相同类型的问题并尝试了他们的解决方案,但是当我调用notifyDatasetChanged()方法时,我的listview没有更新。
这是我的代码
在onCreate()
adapter=new AppointmentAdapter(context, R.layout.appointments_layout,list );
appointments.setAdapter(adapter);
这就是我所谓的notifyDatasetChanged。
JsonDataCallback callbackservice = new JsonDataCallback(Appointments.this, obj) {
@Override
public void receiveData(Object object) {
try {
adapter.clear();
list=new ArrayList<AppontmentBean>();
String userappointments = (String) object;
if(userappointments!=null && userappointments.trim().length()>10)
{
JSONObject appointmentsjson = new JSONObject(userappointments);
JSONArray appoinmentsArray=appointmentsjson.getJSONArray("EMRTable");
for(int i=0;i<appoinmentsArray.length();i++)
{
JSONObject appointment1=appoinmentsArray.getJSONObject(i);
String name=appointment1.getString("PersonLastNameFirstName");
String time=appointment1.getString("StartTime").split("T")[1];
String strDateFormat = "HH:mm a";
String strDateFormat1 = "HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat1);
SimpleDateFormat sdf1 = new SimpleDateFormat(strDateFormat);
Date d=sdf.parse(time);
time=sdf1.format(d);
String reason=appointment1.getString("ReasonForVisit");
list.add(new AppontmentBean(name, time, reason));
}
}
else{
Toast.makeText(context, "No Appointments", Toast.LENGTH_SHORT).show();
}
adapter.notifyDataSetChanged();
我已将调试值添加到列表中,但列表适配器未更新。
但是当我这样使用时,我的列表正在更新
adapter=new AppointmentAdapter(context, R.layout.appointments_layout,list );
appointments.setAdapter(adapter);
使用它是一个好习惯。
编辑: 适配器类
public class AppointmentAdapter extends ArrayAdapter<AppontmentBean> {
@Override
public int getCount() {
// TODO Auto-generated method stub
return countryList.size();
}
Context context;
private ArrayList<AppontmentBean> countryList;
public AppointmentAdapter(Context context, int textViewResourceId, ArrayList<AppontmentBean> countryList) {
super(context, textViewResourceId, countryList);
this.countryList = new ArrayList<AppontmentBean>();
this.countryList.addAll(countryList);
this.context=context;
}
private class ViewHolder {
TextView time;
TextView name;
TextView reason;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
/***
* customizing the view by overriding getview
*/
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.appointments_layout, null);
holder = new ViewHolder();
holder.name=(TextView)convertView.findViewById(R.id.patientName);
holder.time = (TextView) convertView.findViewById(R.id.time);
holder.reason = (TextView) convertView.findViewById(R.id.visitreason);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
AppontmentBean appintments = countryList.get(position);
holder.name.setTypeface(null, Typeface.BOLD);
holder.name.setTag(appintments);
holder.name.setText(appintments.getName());
holder.name.setContentDescription(appintments.getId());
if(appintments.getReason()!=null && appintments.getReason().length()!=0)
{
holder.reason.setText(appintments.getReason());
}
else
{
holder.reason.setVisibility(TextView.GONE);
}
holder.time.setText(appintments.getNumber());
convertView.setContentDescription(appintments.getId());
return convertView;
}
}
class AppontmentBean{
AppontmentBean(String Id,String name,String number,String reason)
{
this.Id=Id;
this.name=name;
this.reason=reason;
this.number=number;
}
String name,number,reason,Id;
/**
* @return the id
*/
public String getId() {
return Id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
Id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the number
*/
public String getNumber() {
return number;
}
/**
* @param number the number to set
*/
public void setNumber(String number) {
this.number = number;
}
/**
* @return the reason
*/
public String getReason() {
return reason;
}
/**
* @param reason the reason to set
*/
public void setReason(String reason) {
this.reason = reason;
}
}
答案 0 :(得分:3)
答案很简单:每次通过list=new ArrayList<AppontmentBean>();
创建新列表时,你的适配器“丢失”对新获取数据的引用,所以你需要做的就是在声明时创建列表对象:
private ArrayList<AppontmentBean> list=new ArrayList<AppontmentBean>();
并在receiveData()
回调中更改此行:
list=new ArrayList<AppontmentBean>();
到
list.clear();
然后适配器将引用单个对象,adapter.notifyDataSetChanged();
将起作用。
编辑:在适配器中完全相同的问题 - 使用addAll()
您将失去对主列表的引用,因此您需要更改这两行:
this.countryList = new ArrayList<AppontmentBean>();
this.countryList.addAll(countryList);
到
this.countryList = countryList;
答案 1 :(得分:0)
只需评论此行
adapter.clear();
再试一次....