我正在尝试更改ListFragment扩展程序的内容,但会发生一些神秘的事情。
在我的MainActivity中,我有ToFragment并致电:
ToFragment.changeContent(newContent);
此功能如下:
public class DataListFragment extends ListFragment {
CustomArrayAdapter mAdapter;
public CustomArrayAdapter getAdapter() {
return mAdapter;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setEmptyText("");
mAdapter = new CustomArrayAdapter(getActivity());
}
public void changeContent(ArrayList<FinalData> data) {
System.out.println("first");
mAdapter.setData1(data);
System.out.println("second");
mAdapter.notifyDataSetChanged();
setListShown(true);
}
}
最后,setData1函数是:
public void setData1(ArrayList<FinalData> data) {
System.out.println("third");
clear();
if (data != null) {
for (FinalData appEntry : data) {
add(appEntry);
}
}
}
当我运行程序时,它调用changeContent()函数,然后打印“first”,但不打印“second”和“third”。然后它什么也没做!没有例外,没有程序中断......它调用mAdapter.setData1(数据);并消失。你有什么想法吗?
UPD: 这是CustomArrayAdapter类:
public class CustomArrayAdapter extends ArrayAdapter<FinalData> {
private final LayoutInflater mInflater;
public CustomArrayAdapter(Context context) {
super(context, android.R.layout.simple_list_item_2);
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void setData1(ArrayList<FinalData> data) {
System.out.println("chch");
clear();
System.out.println("chch");
if (data != null) {
for (FinalData appEntry : data) {
add(appEntry);
System.out.println("chch");
}
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = mInflater.inflate(R.layout.single_item, parent, false);
} else {
view = convertView;
}
FinalData item = getItem(position);
if (position % 2 == 0) {
((TextView)view.findViewById(R.id.train_info)).setEllipsize(TruncateAt.MARQUEE);
((TextView)view.findViewById(R.id.train_info)).setSelected(true);
((TextView)view.findViewById(R.id.stations_info)).setEllipsize(TruncateAt.MARQUEE);
((TextView)view.findViewById(R.id.stations_info)).setSelected(true);
((TextView)view.findViewById(R.id.stations_info)).setText(item.getFullData());
((AutoResizeTextView)view.findViewById(R.id.time_info)).setText(item.getShortData());
((TextView)view.findViewById(R.id.train_info)).setText(item.getTimeData());
((TextView)view.findViewById(R.id.train_info)).setBackgroundColor(Color.parseColor("#6dcaec"));
((TextView)view.findViewById(R.id.stations_info)).setBackgroundColor(Color.parseColor("#a8dff4"));
((AutoResizeTextView)view.findViewById(R.id.time_info)).setBackgroundColor(Color.parseColor("#6dcaec"));
} else {
((TextView)view.findViewById(R.id.train_info)).setEllipsize(TruncateAt.MARQUEE);
((TextView)view.findViewById(R.id.train_info)).setSelected(true);
((TextView)view.findViewById(R.id.stations_info)).setEllipsize(TruncateAt.MARQUEE);
((TextView)view.findViewById(R.id.stations_info)).setSelected(true);
((TextView)view.findViewById(R.id.stations_info)).setText(item.getFullData());
((AutoResizeTextView)view.findViewById(R.id.time_info)).setText(item.getShortData());
((TextView)view.findViewById(R.id.train_info)).setText(item.getTimeData());
((TextView)view.findViewById(R.id.train_info)).setBackgroundColor(Color.parseColor("#cf9fe7"));
((TextView)view.findViewById(R.id.stations_info)).setBackgroundColor(Color.parseColor("#ddbcee"));
((AutoResizeTextView)view.findViewById(R.id.time_info)).setBackgroundColor(Color.parseColor("#cf9fe7"));
}
return view;
}
}