0

我正在使用 ListFragment,但我需要使用 Fragment 来显示列表视图。我如何使用 Fragment 而不是 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);
    }
}
4

1 回答 1

0

看看这个

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

于 2013-04-05T16:08:55.627 回答