我在我的Android应用程序中有一个信号器实例,允许我刷新我的可扩展列表视图(只需添加或删除子项)。
当我在开始时在2个不同的手机中运行我的应用程序它可以正常工作(添加或删除双方的项目),就像我真正想要的那样但是当我开始在不同的活动中导航并且我重复相同的过程时我在开始时做了我的信号器工作(有新的/更新列表),但我的可扩展列表视图不刷新。
这是我的代码:
我的适配器:
public class rtDateAdapter extends AnimatedExpandableListView.AnimatedExpandableListAdapter {
private LayoutInflater inflater;
private List<rtDay> items = new ArrayList<rtDay>();
public rtDateAdapter(Context context, List<rtDay> days) {
inflater = LayoutInflater.from(context);
this.items = days;
}
@Override
public rtDates getChild(int DayPosition, int datePosition) {
return items.get(DayPosition).getDates().get(datePosition);
}
public void refresh(List<rtDay> items)
{
this.items = null;
this.items = items;
**this.notifyDataSetChanged();**
}
@Override
public long getChildId(int dayPosition, int datePosition) {
return items.get(dayPosition).getDates().get(datePosition).getId();
}
@Override
public View getRealChildView(int dayPosition, int datePosition, boolean isLastChild, View convertView, ViewGroup parent) {
rtDatesHolder holder;
rtDates item = getChild(dayPosition, datePosition);
if (convertView == null) {
holder = new rtDatesHolder();
convertView = inflater.inflate(R.layout.date_item_date_layout, parent, false);
holder.startDate = (TextView) convertView.findViewById(R.id.tvStartDate);
holder.finishDate = (TextView) convertView.findViewById(R.id.tvFinishDate);
convertView.setTag(holder);
} else {
holder = (rtDatesHolder) convertView.getTag();
}
holder.startDate.setText(item.getTimeStart());
holder.finishDate.setText(item.getTimeFinish());
return convertView;
}
@Override
public int getRealChildrenCount(int groupPosition) {
return items.get(groupPosition).getDates().size();
}
@Override
public rtDay getGroup(int groupPosition) {
return items.get(groupPosition);
}
@Override
public int getGroupCount() {
return items.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
rtDayHolder holder;
rtDay item = getGroup(groupPosition);
if (convertView == null) {
holder = new rtDayHolder();
convertView = inflater.inflate(R.layout.date_header_fragment_layout, parent, false);
holder.Name = (TextView) convertView.findViewById(R.id.tvHeaderDay);
convertView.setTag(holder);
} else {
holder = (rtDayHolder) convertView.getTag();
}
holder.Name.setText(item.getName());
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
private static class rtDatesHolder {
TextView startDate;
TextView finishDate;
}
private static class rtDayHolder {
TextView Name;
}
}
我尝试在我的适配器内部使用一个方法强制执行this.notifyDataSetChanged,但不起作用。
这是我的片段:
public class DateFragment extends Fragment {
private View view;
private List<rtDay> Days = new ArrayList<rtDay>();
private Gson gson = new Gson();;
private rtDateAdapter dateAdapter;
private AnimatedExpandableListView listView;
private int DoctorSelectedId;
HubProxy scheduleHub;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.date_fragment_layout, container, false);
DoctorSelectedId = KimchiDataCache.getInstance().pop(2);
return view;
}
public void onResume() {
scheduleHub = HubConnectionFactory.getInstance().getHub();
SubscribeUpdateDates(scheduleHub);
.....
Request request = rtRequestHttpFactory.GetRequestWithParameter(ServerURLServices.PATH_DATES, ServerURLServices.PARAM_FUNCTIONARY_ID, Integer.toString(DoctorSelectedId));
try {
String json = new rtServerRequestBackground().execute(request).get();
Days = gson.fromJson(json, new TypeToken<ArrayList<rtDay>>(){}.getType());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
dateAdapter = new rtDateAdapter(getActivity(), Days);
listView = (AnimatedExpandableListView) view.findViewById(R.id.listDaysAndDates);
listView.setAdapter(dateAdapter);
listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
if (listView.isGroupExpanded(groupPosition)) {
listView.collapseGroupWithAnimation(groupPosition);
} else {
listView.expandGroupWithAnimation(groupPosition);
}
return true;
}
});
listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
......
}
});
super.onResume();
}
@SuppressWarnings("unused")
private void SubscribeUpdateDates(HubProxy scheduleHub) {
scheduleHub.on("UpdateDoctorDates", new SubscriptionHandler1<rtDay[]>() {
@Override
public void run(final rtDay[] days) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Days.clear();
for (rtDay h : days) {
Days.add(h);
}
dateAdapter.refresh(Days);
dateAdapter.notifyDataSetChanged(); //ONLY WORKS AT THE BEGING WHEN I RUN MY APP
}
});
}
}, rtDay[].class);
}
}
解决方案
我终于解决了问题,我将我的片段类更改为:
public class DateFragment extends Fragment {
private View view;
private List<rtDay> Days;
private Gson gson;
private rtDateAdapter dateAdapter;
private AnimatedExpandableListView listView;
private int DoctorSelectedId;
HubProxy scheduleHub;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.date_fragment_layout, container, false);
scheduleHub = HubConnectionFactory.getInstance().getChatHub();
gson = new Gson();
DoctorSelectedId = KimchiDataCache.getInstance().pop(2);
JoinGroup(scheduleHub);
SubscribeUpdateDates(scheduleHub);
Request request = rtRequestHttpFactory.GetRequestWithParameter(ServerURLServices.PATH_DATES, ServerURLServices.PARAM_FUNCTIONARY_ID, Integer.toString(DoctorSelectedId));
try {
String json = new rtServerRequestBackground().execute(request).get();
Days = gson.fromJson(json, new TypeToken<ArrayList<rtDay>>(){}.getType());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
dateAdapter = new rtDateAdapter(getActivity(),Days);
listView = (AnimatedExpandableListView) view.findViewById(R.id.listDaysAndDates);
listView.setAdapter(dateAdapter);
// In order to show animations, we need to use a custom click handler
// for our ExpandableListView.
listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
// We call collapseGroupWithAnimation(int) and
// expandGroupWithAnimation(int) to animate group
// expansion/collapse.
if (listView.isGroupExpanded(groupPosition)) {
listView.collapseGroupWithAnimation(groupPosition);
} else {
listView.expandGroupWithAnimation(groupPosition);
}
return true;
}
});
listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
rtDay day = Days.get(groupPosition);
rtDates date = Days.get(groupPosition).getDates().get(childPosition);
Toast.makeText(getActivity(), "Reservando Cita: " + date.getTimeStart() + " - " + date.getTimeFinish(), Toast.LENGTH_LONG).show();
ActionDialogFragment dialog = ActionDialogFragment.newInstance(getActivity(), ActionEnum.SCHEDULEBYPACIENT, day.getName(),date.getId(),date.getTimeStart(),date.getTimeFinish());
dialog.show(getFragmentManager(), "dialog");
return false;
}
});
return view;
}
private void SubscribeUpdateDates(HubProxy scheduleHub) {
scheduleHub.on("UpdateDoctorDates", new SubscriptionHandler1<rtDay[]>() {
@Override
public void run(final rtDay[] days) {
if (getActivity() != null) {
//THE PROBLEM WA HERE, getActivity().runOnUiThread WAS NULL SOMETIMES I GOT A NULLEXCEPTION(getActivity().runOnUiThread WAS NULL) BUT THE APP WAS STILL RUNNING SMOOTHLY DESPITE THIS, SO I JUST ADD IF STATEMENT BEFORE THIS AND FIX THE PROBLEM, NOW MY LISTVIEW REFRESH WITHOUT PROBLEM.
getActivity().runOnUiThread(new Runnable() {
public void run() {
Days.clear();
for (rtDay h : days) {
Days.add(h);
}
dateAdapter.notifyDataSetChanged();
}
});
}
else{
Days.clear();
for (rtDay h : days) {
Days.add(h);
}
dateAdapter.notifyDataSetChanged();
}
}
}, rtDay[].class);
}
private void JoinGroup(HubProxy scheduleHub) {
scheduleHub.invoke("JoinFunctionaryDatesGroup", DoctorSelectedId);
}
@Override
public void onPause() {
// HubProxy Hub = HubFactory.getInstance().getScheduleHub();
scheduleHub = HubConnectionFactory.getInstance().getChatHub();
scheduleHub.invoke("LeaveFunctionaryDatesGroup", DoctorSelectedId);
super.onPause();
}
}