2013-04-29 103 views
0

我試圖在DataListFragment(擴展ListFragment)中顯示新數據。這是我如何做這裏面MainActivity:未顯示ListFragment數據

  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不是空的,但列表沒有改變,它仍然是空的,沒有看到任何東西。該怎麼做?

UPD:我設法讓我的程序就行了

setListShown(true); 

它只是沒有錯誤,並停止後,不要做任何停止。這是神祕...

+0

您在*'changeAdapter()'之前調用'notifyDataSetChanged()'*,因此不會通知Fragment。 – 2013-04-29 14:49:54

回答

1

要調用notifyDataSetChanged()changeAdapter()之前,所以Fragment不會更改的通知。您應該創建一個適配器實例,並且只需撥打setListAdapter()一次(最有可能在片段的onCreate()中)。然後從您的活動中隨時撥打ToFragment.getListAdapter().notifyDataSetChanged()