Reputation: 20155
I have working with custom list view many times but I haven't faced this problem.. I see lot questions of same type and tried their solutions but my listview isn't updating when I call notifyDatasetChanged() method.
here is my code
in onCreate()
adapter=new AppointmentAdapter(context, R.layout.appointments_layout,list );
appointments.setAdapter(adapter);
and this is where I have called 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();
I have debugged values are added to the list but the list adapter doesn't updating.
But when I use like this my list is updating
adapter=new AppointmentAdapter(context, R.layout.appointments_layout,list );
appointments.setAdapter(adapter);
is it a good practice to use this.
Edit: Adapter class
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;
}
}
Upvotes: 1
Views: 1127
Reputation: 3915
Answer is simple: each time you create new list by list=new ArrayList<AppontmentBean>();
your adapter "loses" reference to new fetched data, so all you need to do to get it work is create list object on declaration:
private ArrayList<AppontmentBean> list=new ArrayList<AppontmentBean>();
and change this line in receiveData()
callback:
list=new ArrayList<AppontmentBean>();
to
list.clear();
then adapter would have reference to single object, and adapter.notifyDataSetChanged();
will works.
EDIT: and absolutely same problem in adapter - by using addAll()
you are losing reference to main list so you need to change this two lines:
this.countryList = new ArrayList<AppontmentBean>();
this.countryList.addAll(countryList);
to
this.countryList = countryList;
Upvotes: 3