2013-04-05 79 views
0

我正在使用ListFragment,但我需要使用Fragment來顯示列表視圖。我如何使用Fragment而不是ListFragment PLZ幫助我。另外給我一些解釋哪一個更好用。 我想使用擴展片段,而不是*寬ListFragment *擴展片段而不是ListFragment

import java.util.ArrayList; 

import android.app.Activity; 
import android.app.ListFragment; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ListView; 
import android.widget.Toast; 

public class CountryListFragment extends ListFragment{ 

    ArrayList<Video> videos; 
    Activity mactivity; 

    /** List of countries to be displayed in the ListFragment */ 

    ListFragmentItemClickListener ifaceItemClickListener; 

    /** An interface for defining the callback method */ 
    public interface ListFragmentItemClickListener { 
     /** This method will be invoked when an item in the ListFragment is clicked */ 
     void onListFragmentItemClick(int position); 
    } 

    /** A callback function, executed when this fragment is attached to an activity */ 
    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     mactivity=activity; 

     try{ 
      /** This statement ensures that the hosting activity implements ListFragmentItemClickListener */ 
      ifaceItemClickListener = (ListFragmentItemClickListener) activity;   
     }catch(Exception e){ 
      Toast.makeText(activity.getBaseContext(), "Exception",Toast.LENGTH_SHORT).show(); 
     } 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     new SubTask().execute(); 
    } 

    private class SubTask extends AsyncTask<Void, Void, ArrayList<Video>>{ 

     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 
     } 

     @Override 
     protected ArrayList<Video> doInBackground(Void... params) { 
      //Crearte instance to http call 
      GetHttpCall getCall=new GetHttpCall(); 
      String response=null; 
      try { 
       response=getCall.connect(); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      JSONParser _parser=new JSONParser(); 
      _parser.parseHttpJson(response); 

      String [] item = JSONParser.num_title; 
      int []id=JSONParser.num_id; 

      videos=new ArrayList<Video>(); 

      for(int i=0;i<item.length;i++) 
      { 
       Video vid=new Video(); 
       vid.setTitle(item[i]); 
       vid.setId(id[i]); 

       videos.add(vid); 
      } 

      return videos; 
     } 

     @Override 
     protected void onProgressUpdate(Void... values) { 
      // TODO Auto-generated method stub 
      super.onProgressUpdate(values); 
     } 

     @Override 
     protected void onPostExecute(ArrayList<Video> result) { 
      super.onPostExecute(result); 
      BindDataAdapter adapter = new BindDataAdapter(mactivity, videos); 
      setListAdapter(adapter); 
     } 
    } 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) {  
     /** Invokes the implementation of the method onListFragmentItemClick in the hosting activity */ 
     ifaceItemClickListener.onListFragmentItemClick(position); 
    } 
} 
+1

「但我需要使用Fragment來顯示列表視圖」 - 爲什麼? – CommonsWare 2013-04-05 16:03:08

+0

只是想看到差異 – Nik 2013-04-05 19:40:00

回答

0

退房this

克隆它,在你的IDE創建項目,並期待在示例應用程序「TestBedDSLV」以及它如何使用「DSLVFragment」和ListFragment。