0

我有一個主要活動和一個包含作爲該主要活動選項卡的自定義ArrayAdapter的ListFragment。主要活動有一個AsyncTask,它可以下拉一個xml文件,然後調用另一個類來在應用程序啓動時解析它。 ListFragment數據來自AsyncTask的解析數據。我希望能夠調用ListFragment的自定義適配器,以便我可以在AsyncTask的onPostExecute方法中通知DataSetChanged。我怎樣才能做到這一點?從另一個活動調用自定義ArrayAdapter方法

現在,當我打開應用程序時,數據被拉取並解析,但ListView爲空,因爲在AyncTask完成後,尚未調用notifyDataSetChanged。

代碼被公佈如下:

MainActivity

public class MainActivity extends ActionBarActivity { 
    private static final String TAG = MainActivity.class.getSimpleName(); 

    private ActionBar.Tab tab1, tab2; 

    private Fragment fragmentChannels = new ChannelListFragment(); 
    private Fragment fragmentTab2 = new ChannelListFragment(); 

    private ArrayList<Channel> mChannels; 
    private ArrayList<Programme> mProgrammes; 
    private ProgressDialog mProgressDialog; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ActionBar actionBar = getSupportActionBar(); 
     actionBar.setTitle(R.string.app_name); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     actionBar.addTab(actionBar.newTab().setText(R.string.channels_title).setTabListener(new TabListener(fragmentChannels))); 
     actionBar.addTab(actionBar.newTab().setText("tab 2").setTabListener(new TabListener(fragmentTab2))); 

     // Get singleton 
     mChannels = ChannelStore.get(getApplicationContext()).getChannels(); 
     // Populate singleton 
     populateChannels(); 
    } 

    private void populateChannels() { 
     if (CheckNetwork.isInternetAvailable(getApplication())) { 
      ChannelSetup cs = new ChannelSetup(); 
      cs.execute(); 
     } else { 
      Toast.makeText(getApplication(), "No internet connection!", Toast.LENGTH_LONG).show(); 
     } 
    } 

    private class ChannelSetup extends AsyncTask<Void, String, String> { 

     @Override 
     protected String doInBackground(Void... params) { 
      // download xml file 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      try { 
      // parse the xml file and create objects from it. 

      // need to update the listadapter of the listfragment somehow 
     } 
    } 
} 

ChannelListFragment

public class ChannelListFragment extends ListFragment { 
    private static final String TAG = "ChannelListFragment"; 

    private ArrayList<Channel> mChannels; 
    private ChannelAdapter mAdapter; 
    private Date mNow; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getActivity().setTitle(R.string.channels_title); 
     mNow = new Date(); 

     mChannels = ChannelStore.get(getActivity()).getChannels(); 
     Log.i(TAG, "" + mChannels.size() + " " + ""); 

     mAdapter = new ChannelAdapter(mChannels); 
     setListAdapter(mAdapter); 
    } 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
     Channel c = (Channel) (getListAdapter()).getItem(position); 
     Log.i(TAG, "CH" + c.getId() + " clicked"); 

     Intent i = new Intent(getActivity(), ChannelDetailsActivity.class); 
     i.putExtra(ChannelDetailsFragment.EXTRA_CHANNEL_ID, c.getId()); 
     startActivity(i); 
    } 

    public class ChannelAdapter extends ArrayAdapter<Channel> { 
     public ChannelAdapter(ArrayList<Channel> channels) { 
      super(getActivity(), 0, channels); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      // If we weren't given a view, inflate one 
      if (convertView == null) { 
       convertView = getActivity().getLayoutInflater().inflate(
         R.layout.list_item_channel_table, null); 
      } 

      // do stuff with various text views, etc 

      return convertView; 
     } 

     public void updateListData() { 
      this.clear(); 
      this.addAll(mChannels); 
      this.notifyDataSetChanged(); 
     } 
    } 

} 
+0

你如何將你的片段附加到你的活動? – mmlooloo 2014-08-31 19:30:25

+0

我在第一個代碼片段中設置了導航模式後,通過actionbar.addTab()附加了片段 – aaearon 2014-08-31 20:03:00

回答

0

我建議你在你的ChannelListFragment創建一個公共方法refreshChannels()可更新ChannelAdapter內容(獲取渠道來自ChannelStore)並在notifyDataSetChanged()之後致電。您可以在AsyncTask onPostExecute()方法上調用此方法。

+0

這就是我的想法,但是當我嘗試它時說refreshChannels()需要是靜態的,我不知道如何我會在我的適配器中調用notifydatasetchanged()方法,公共方法 – aaearon 2014-08-31 20:04:00

+0

@aaearon,也許問題是你試圖調用'ChannelListFragment.refreshChannels()'而不是調用對象(你已經有一個全局變量'fragmentChannels ') - >'fragmentChannels.refreshChannels()'。 (你的公共方法不是必須的,因爲你會用對象來調用它) – MartinCR 2014-08-31 20:19:42

相關問題