2

我正在尝试使用 cwac 提供的演示......但是当我将它拉入我的片段时,我收到错误“方法 getActivity() 未定义类型 DataAdapter

我确信这是一个上下文问题,所以我尝试了多种方法来引用数据适配器,并确保导入了正确的支持文件(即 v4)。

import java.util.ArrayList;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ArrayAdapter;

import com.commonsware.cwac.endless.EndlessAdapter;
import com.commonsware.cwac.endless.demo.EndlessAdapterCustomTaskFragment;

public class Fragment2 extends ListFragment {

    DataAdapter adapter=null;
    ArrayList<Integer> items=null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_dod_events, container, false);

        setRetainInstance(true);

        if (adapter == null) {
          items=new ArrayList<Integer>();

          for (int i=0; i < 25; i++) {
            items.add(i);
          }

          adapter=new DataAdapter(items);
          //adapter = new DataAdapter(getActivity(), items);
          adapter.setRunInBackground(false); // Tell the adapter
                                             // we will handle
                                             // starting the
                                             // background task
        } 

        setListAdapter(adapter);        

        // Do Stuff Here
        return root;
    }

    @Override public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Save some state!
    }
}


class DataAdapter extends EndlessAdapter implements
IItemsReadyListener {
    private RotateAnimation rotate=null;

    DataAdapter(ArrayList<Integer> list) {

        super(new ArrayAdapter<Integer>(getActivity(), R.layout.row,
                android.R.id.text1, list));

        rotate=
                new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
                        0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotate.setDuration(600);
        rotate.setRepeatMode(Animation.RESTART);
        rotate.setRepeatCount(Animation.INFINITE);
    }

    @Override
    protected View getPendingView(ViewGroup parent) {
        View row=
                getActivity().getLayoutInflater().inflate(R.layout.row, null);

        View child=row.findViewById(android.R.id.text1);

        child.setVisibility(View.GONE);

        child=row.findViewById(R.id.throbber);
        child.setVisibility(View.VISIBLE);
        child.startAnimation(rotate);

        return(row);
    }

    @Override
    protected boolean cacheInBackground() throws Exception {
        new FetchDataTask(this, items.size()).execute();

        return(items.size()<75);
    }

    @Override
    public void onItemsReady(ArrayList<Integer> data) {
        items.addAll(data);
        adapter.onDataReady(); // Tell the EndlessAdapter to
        // remove it's pending
        // view and call
        // notifyDataSetChanged()
    }

    @Override
    protected void appendCachedData() {
    }
}

interface IItemsReadyListener {
    public void onItemsReady(ArrayList<Integer> data);
}

class FetchDataTask extends AsyncTask<Void, Void, ArrayList<Integer>> {
    IItemsReadyListener listener;

    /*
     * The point from where to start counting. In a real
     * life scenario this could be a pagination number
     */
    int startPoint;

    protected FetchDataTask(IItemsReadyListener listener, int startPoint) {
        this.listener=listener;
        this.startPoint=startPoint;
    }

    @Override
    protected ArrayList<Integer> doInBackground(Void... params) {
        ArrayList<Integer> result=new ArrayList<Integer>();

        SystemClock.sleep(3000); // pretend to do work
        for (int i=startPoint; i < startPoint + 25; i++) {
            result.add(i);
        }

        return(result);
    }

    @Override
    protected void onPostExecute(ArrayList<Integer> result) {
        listener.onItemsReady(result);
    }
}
4

1 回答 1

1

尝试以这种方式调用 getActivity()

Fragment2.getActivity();

内部类内部。

于 2013-06-09T23:09:16.633 回答