我正在尝试更改我的 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(data); 并消失。你有什么想法?
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;
}
}