而是扩展Fragment而不是ListFragment

时间:2013-04-05 16:00:20

标签: android

我正在使用ListFragment,但我需要使用Fragment来显示listview。我如何使用Fragment代替ListFragment plz帮助我。还给我一些解释,哪个更好用。 我想使用extends Fragment代替* extends 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 个答案:

答案 0 :(得分:0)

结帐this

克隆它,在IDE中创建项目并查看示例应用程序'TestBedDSLV'以及它如何使用'DSLVFragment'和ListFragment。