1

我曾多次使用自定义列表视图,但我没有遇到过这个问题。我看到很多相同类型的问题并尝试了他们的解决方案,但是当我调用 notifyDatasetChanged() 方法时,我的列表视图没有更新。

这是我的代码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;
    }

}
4

2 回答 2

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;
于 2013-01-31T12:07:41.810 回答
0

只需评论这一行

adapter.clear();

然后再试一次....

于 2013-01-30T06:47:40.997 回答