请建议我正确使用notifyDataSetChanged()

时间:2016-04-25 06:39:10

标签: android listview custom-adapter notifydatasetchanged

我使用带有滑动标签布局的视图寻呼机。并且我使用自定义适配器显示数据的列表视图。并且还列出了列表视图,我有一个列表视图详细活动,其中我更详细地显示数据。在这些详细活动中,我对数据进行了一些更改(某些后期方法)。之后,我创建了一个customAdapter类的实例,并调用notifyDataSetChanged()以刷新列表视图。我在这里的问题是列表视图有时会快速刷新,有时会延迟几秒钟。 因此,有人可以建议我正确使用列表视图以及需要进行哪些更改才能在执行post方法时刷新列表视图。 我的代码片段类:

private void showJsonData(String response) {

    try {
        String serviceID = LoggedInUserStore.getLoggedInServiceId(getContext());
        List<Complaint> userList = new ArrayList<>(); //ArrayList of type user(POJO CLASS)
        JSONArray jsonArray = new JSONArray(response);

        for (int i = 0; i < jsonArray.length(); i++) {
            if (serviceID.equals(jsonArray.getJSONObject(i).getString("ServiceID"))) {
                if (jsonArray.getJSONObject(i).getString("CallStatusID").equalsIgnoreCase("1")) {
                    userList.add(0, Complaint.fromJson(jsonArray.getJSONObject(i))); //
                }
            }

        }

        assignAdapter = new AssignAdapter(getActivity(), userList);
        listView.setAdapter(assignAdapter);
        listView.invalidateViews();
        assignAdapter.notifyDataSetChanged();

    } catch (JSONException e) {
        e.printStackTrace();
    }

}

My custom adapter class
public class AssignAdapter extends BaseAdapter implements Filterable {
List<Complaint> ticket = new ArrayList<>();
private Context context;
String ticketNo, complaint, raiseDate;
Complaint user;
List<Complaint> temporaryList = new ArrayList<>();

/*String status, priority;*/

public AssignAdapter(Context context, List<Complaint> ticket) {
    this.context = context;
    this.ticket = ticket;
    this.temporaryList = ticket;
}

@Override
public int getCount() {
    return temporaryList.size();
}

@Override
public Object getItem(int position) {
    return temporaryList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

public class viewHolderItem {
    TextView ticketNumberText, complaintNameText, raisedDateText;
}

//Set the layout for the fragment and return it.
@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    viewHolderItem holder;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.custom_list_view, null, true);

        holder = new viewHolderItem();
        holder.ticketNumberText = (TextView) convertView.findViewById(R.id.ticketIdSupervisor);
        holder.complaintNameText = (TextView) convertView.findViewById(R.id.complaintNameSupervisor);
        convertView.setTag(holder);
    } else {
        holder = (viewHolderItem) convertView.getTag();
    }

    user = temporaryList.get(position);
    if (user != null) {
        //Get the Ticket Number
        Typeface custom_font = Typeface.createFromAsset(context.getAssets(), "fonts/DroidSerif.ttf");
        ticketNo = temporaryList.get(position).getTicketNumber();
        holder.ticketNumberText.setText(ticketNo);
        holder.ticketNumberText.setTag("ticketNumber");
        holder.ticketNumberText.setTypeface(custom_font);


        //Get the complaint Name
        complaint = temporaryList.get(position).getComplaintDetails();
        holder.complaintNameText.setText(complaint);
        holder.complaintNameText.setTag("complaint");
        holder.complaintNameText.setTypeface(custom_font);

    }
    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(context.getApplicationContext(), ComplaintDetailsSupervisor.class);
            i.putExtra("COMPLAINT NAME", temporaryList.get(position).getComplaintDetails());
            i.putExtra("RAISED DATE", temporaryList.get(position).getRaisedDate().substring(0, 10));
            context.startActivity(i);
        }
    });
    notifyDataSetChanged();
    return convertView;
}
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
}

}

我的列表视图详细活动类

 dialogButtonOk.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            assignComplaint();
                            al.remove(position);

                            AssignAdapter assignAdapter = new AssignAdapter(getApplicationContext(), al);
                            assignAdapter.notifyDataSetChanged();

                            ComplaintDetailsSupervisor.this.finish();


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

                dialogButtonNo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.show();
            }
        }
    });


}

public void assignComplaint() throws JSONException {
    //my custom method...
}

在列表视图详细活动类中,我正在执行此操作

al.remove(position);
AssignAdapter assignAdapter = new AssignAdapter(getApplicationContext(), al);
assignAdapter.notifyDataSetChanged();
ComplaintDetailsSupervisor.this.finish();

删除列表视图的位置并立即调用适配器。这样可以正常工作,但我不知道为什么有时它不刷新。可能是当列表视图有一个项目时它不会立即刷新。

1 个答案:

答案 0 :(得分:0)

您正在创建一个新的适配器并在其上调用notifyDatasetChanged但未使用新适配器作为参数调用setAdapter,因此您的列表不会刷新。

您需要致电

setAdapter(assignAdapter)

或重复使用现有的assignAdapter,然后在其上调用notifyDatasetChanged()