0

我有一个寻呼机适配器,它将像这样调用 ListFragment:

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

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

        return fragment;

然后我有一个 ListActivity,我想将其更改为 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);
    }
}

但我真的不能让它发挥作用..我试过了:

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;      

    }
}

更新: - 我只是回答我自己的问题,并制作了一个小教程来解释初学者如何通过示例将 ListAdapter 实现到您的项目中。

我从一个示例中得到了这个 ListAdapter,只需将文件复制到我的项目中,如果你运行它,就会崩溃。

因此,您需要按照我的回答进行更改并实施ListArrayAdapter您在互联网上找到的任何内容。

4

3 回答 3

1

最后我把它付诸实践了。

好吧,首先您需要更正来自 的调用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;

FragmentListArraySupportFragmentActivityArrayListFragmentListFragment。_

这是FragmentListArraySupport代码:

    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);
            }
        }
   }

看到你需要更改ListActivityFragmentActivity 现在你的图像适配器工作了,所以无论ListArrayAdapter你在互联网上找到什么,你只需要复制类文件,调用ArraySupportPagerAdapter并将.ListActivityFragmentActivity

于 2012-04-21T16:24:01.173 回答
0

查看错误消息,然后查看您的onCreateView()-method。错误消息说您正在尝试将 a 添加View已经有父级的父级。

你得到的原因IllegalStateException是因为你试图(ViewGroup) container通过returncontainer你的onCreateView(...). 当然container已经有一个父母,这就是你得到错误的原因。

此外,onCreateView() 中有关适配器的部分可能会引发错误,因为在ListFragment您设置适配器之前您还没有适配器。调用((ImageAdapter) getListAdapter())will(可能)返回 null,导致.getImageDownloader()(可能)抛出一个NullPointerException. 在您的 中尝试以下操作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
}

更新

另外,将您的更改ListActivityActivity(或者FragmentActivity如果您使用支持库)。setListAdapter(new ImageAdapter());从您的中删除对 的调用Activity,然后将 移动implements RadioGroup.OnCheckedChangeListenerListFragment

于 2012-04-21T13:55:58.137 回答
0

如果没有关于“崩溃”的任何细节,很难确定出了什么问题。但是,我最好的猜测是,这是NullPointerException因为您似乎正在尝试从 的ListFragment适配器获取图像下载器,而适配器尚未初始化。

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

如果没有提前调用setListAdapter(...),getListAdapter()上面代码的第一行将返回null. 显然,这意味着您无法从中检索任何内容。我想你会想把它改成这样:

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

设置完毕后,可能还有其他原因导致您的问题,但是如果没有任何具体的堆栈跟踪,我们只能进行猜测......

于 2012-04-21T03:09:09.543 回答