0

我正在尝试在我的 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);

它只是停止而没有错误,并且在它之后不做任何事情。很神秘...

4

1 回答 1

1

notifyDataSetChanged() 之前 changeAdapter()打过电话,因此Fragment不会收到更改通知。您应该创建适配器的单个实例并setListAdapter()只调用一次(很可能在片段中onCreate())。然后从您的活动中,只需ToFragment.getListAdapter().notifyDataSetChanged()在适当的时候调用。

于 2013-04-29T14:51:49.807 回答