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();ます。

EDIT : アダプターのまったく同じ問題 - を使用addAll()すると、メイン リストへの参照が失われるため、次の 2 行を変更する必要があります。

 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 に答える