How to implements a ListFragment into your project from a Sample?

时光毁灭记忆、已成空白 提交于 2020-01-01 20:27:11

问题


I have a Pager adapter that will call a ListFragment like this:

public Fragment getItem(int position) {
        Fragment fragment = new ListViewFragment();

        // set arguments here, if required
        Bundle args = new Bundle();
        fragment.setArguments(args);

        return fragment;

Then I have a ListActivity that I want to change to ListViewFragment.

public class ImageListActivity extends ListActivity  implements RadioGroup.OnCheckedChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.imagelist);

        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(this);

        setListAdapter(new ImageAdapter());
    }

    public void onCheckedChanged(RadioGroup group, int checkedId) {
        ImageDownloader.Mode mode = ImageDownloader.Mode.NO_ASYNC_TASK;

        if (checkedId == R.id.correctButton) {
            mode = ImageDownloader.Mode.CORRECT;
        }else if (checkedId == R.id.randomButton) {
                mode = ImageDownloader.Mode.NO_DOWNLOADED_DRAWABLE;
        }

        ((ImageAdapter) getListAdapter()).getImageDownloader().setMode(mode);
    }
}

But I really cant put it to work.. Ive tried that:

public class ListViewFragment extends ListFragment {
    int mNum;
    Context ctx;
    View v;
    static int p; 

    /**
     * Create a new instance of CountingFragment, providing "num"
     * as an argument.
     */
    static ListFragment newInstance(int num) {
        ListFragment f = new ListFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {


          v = inflater.inflate(R.layout.imagelist, container, false);
          ImageDownloader.Mode mode = ImageDownloader.Mode.NO_ASYNC_TASK;    
          mode = ImageDownloader.Mode.CORRECT;
          ((ImageAdapter) getListAdapter()).getImageDownloader().setMode(mode);
          setListAdapter(new ImageAdapter());            

          return container;      

    }
}

UPDATE: - I just answer my own question and I make a little tutorial to explain beginners how to implements a ListAdapter into your project from a sample.

I´ve got this ListAdapter from a sample, and just copy the files to my project, if you run it, will crash.

So, you need to follow my answer and made the changes and implements whatever ListArrayAdapter that you found in internet.


回答1:


Finally I´ve put it to work.

Well, first you need to correct the call from PagerAdapter.

public Fragment getItem(int position) {
        Fragment fragment = new FragmentListArraySupport.ArrayListFragment();

        // set arguments here, if required
        Bundle args = new Bundle();
        fragment.setArguments(args);

        return fragment;

The FragmentListArraySupport is the FragmentActivity and ArrayListFragment is the ListFragment.

This is the FragmentListArraySupport code:

    public class FragmentListArraySupport extends FragmentActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            setTheme(SampleList.THEME); //Used for theme switching in samples
            super.onCreate(savedInstanceState);

            // Create the list fragment and add it as our sole content.
            if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
                ArrayListFragment list = new ArrayListFragment();
                getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();
            }
        }
        public static class ArrayListFragment extends ListFragment {

            @Override
            public void onActivityCreated(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
                ImageDownloader.Mode mode = ImageDownloader.Mode.CORRECT;
                ImageAdapter imageAdapter = new ImageAdapter();
                imageAdapter.getImageDownloader().setMode(mode);
                setListAdapter(imageAdapter); 
            }

            @Override
            public void onListItemClick(ListView l, View v, int position, long id) {
                Log.i("FragmentList", "Item clicked: " + id);
            }
        }
   }

See that you need to change ListActivity to FragmentActivity Now you have your image adapter working, so whatever ListArrayAdapter that you found around internet you just need to copy the class files, call the ArraySupport in the PagerAdapter and change the ListActivity to FragmentActivity.




回答2:


Without any specifics on the 'crash' it's kind of hard to pinpoint what's going wrong. However, my best guess would be that it's a NullPointerException because you appear to be trying to get the image downloader from the ListFragment's adapter while the adapter hasn't been initialized yet.

((ImageAdapter) getListAdapter()).getImageDownloader().setMode(mode);
setListAdapter(new ImageAdapter());  

Without an earlier call of setListAdapter(...), getListAdapter() on the first line in above code will return null. Obviously that means you cannot retrieve anything from it. I imagine you will want to change it to something like this:

ImageAdapter imageAdapter = new ImageAdapter();
imageAdapter.getImageDownloader().setMode(mode);
setListAdapter(imageAdapter);  

That being set, there may be other things causing your problems, but without any concrete stack traces we can only make guesses...




回答3:


Take a look at the error message and then take a look at your onCreateView()-method. The error message says you're trying to add a View to a parent, that already has a parent.

The reason you're getting the IllegalStateException is because you're trying to add (ViewGroup) container as a child to itself by return-ing container in your onCreateView(...). Of course container already has a parent, which is why you got the error.

Also, the part about the adapter in onCreateView() can throw an error, since your ListFragment doesn't have an adapter yet until you set one. The call to ((ImageAdapter) getListAdapter()) will (probably) return null, causing .getImageDownloader() to (probably) throw a NullPointerException. Try the following in your Fragment:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        // You should choose what mode you want,
        // ImageDownloader.Mode.NO_ASYNC_TASK or
        // ImageDownloader.Mode.CORRECT;... no use in setting it, just
        // to override it again 1 line later :p
        ImageDownloader.Mode mode = ImageDownloader.Mode.NO_ASYNC_TASK;
        mode = ImageDownloader.Mode.CORRECT;

        ImageAdapter mImageAdapter = new ImageAdapter();
        mImageAdapter.setMode(mode);

        setListAdapter(mImageAdapter);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

      v = inflater.inflate(R.layout.imagelist, container,           

      return v; // notice you're returning your inflated View here,
                // instead of container
}

Update

Also, change your ListActivity to an Activity (or FragmentActivity if you use the support library). Remove the call to setListAdapter(new ImageAdapter()); from your Activity, and move the implements RadioGroup.OnCheckedChangeListener to the ListFragment.



来源:https://stackoverflow.com/questions/10255094/how-to-implements-a-listfragment-into-your-project-from-a-sample

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!