如何在片段"安卓工作室"中停止"for loop"?



我在带有选项卡布局的片段中有列表视图,问题是当我使用 for 循环将数据放入 listFragment 时,如果我在选项卡或片段之间移动,它永远不会停止,该项目将重新出现,这是我的代码。 感谢您的帮助。

启动时

当我在选项卡之间移动时

int [] imag = {R.drawable.current, R.drawable.current, R.drawable.current, R.drawable.current, R.drawable.current};
    String[] storynum = {"11111", "22222", "33333", "444444",
            "555555"};
    ArrayList<HashMap<String, String>> lst = new ArrayList<HashMap<String, String>>();
    SimpleAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        HashMap<String, String> hmap = new HashMap<String, String>();
        for (int i=0; i<imag.length; i++){
            hmap = new HashMap<String, String>();
            hmap.put("storynum", storynum[i]);
            hmap.put("imag",Integer.toString(imag[i]));
            lst.add(hmap);
        }

        String[] in = {"storynum","imag"};
        int[] out = {R.id.txtloc, R.id.imgvi};
        adapter = new SimpleAdapter(getActivity(), lst,R.layout.listview_layout, in, out);
        setListAdapter(adapter);
        return super.onCreateView(inflater, container, savedInstanceState);
    }
首先,

你想稍微清理一下你的格式吗?(很多新行(,这可以帮助您获得更多的回应和更少的反对票。

其次,我认为问题可能与onCreateView的实现有关。

在"onCreateView"行下,您可以添加以下内容:

 ViewGroup view = (ViewGroup) inflater.inflate(R.layout.fragment_announcements_page, container, false)

将R.layout.your_fragment_layout_here替换为您自己的。

那么你的返回语句应该只是:

return view;

我不知道你的返回语句是什么/做什么,但我认为它只是无休止地调用 CreateView。

最新更新