我正在尝试在DataListFragment中显示新数据(扩展ListFragment)。以下是我在MainActivity中的使用方法:
CustomArrayAdapter adapter = new CustomArrayAdapter(this);
adapter.setData1(newContent);
adapter.notifyDataSetChanged();
ToFragment.changeAdapter(adapter);
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;
}
}
DataListFragment看起来像:
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());
mAdapter.notifyDataSetChanged();
setListShown(true);
}
public void changeAdapter(CustomArrayAdapter adapter) {
mAdapter = adapter;
setListAdapter(adapter);
mAdapter.notifyDataSetChanged();
setListShown(true);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
}
}
我确信newContent不是空的,但是List没有改变,它仍然是空的,没有看到任何东西。怎么办?
UPD:我已经设法让我的程序在线上停止
setListShown(true);
它只是在没有错误的情况下停止,并且在它之后不做任何事情。这很神秘......
答案 0 :(得分:1)
您在 notifyDataSetChanged()
之前呼叫changeAdapter()
,因此Fragment
不会收到有关更改的通知。您应该创建适配器的单个实例并仅调用setListAdapter()
一次(很可能在片段的onCreate()
中)。然后,在您的活动中,只需在适当的时候致电ToFragment.getListAdapter().notifyDataSetChanged()
。