在的ListView片段不显示 [英] ListView in Fragment Not Displayed

查看:140
本文介绍了在的ListView片段不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我那里有几个选项卡的申请;与任何一种相互作用呼吁要显示的片段。不幸的是,当我切换到下面的片段,我的ListView没有出现,尽管有问题的列表中填充。非常感谢您的帮助,您可以提供。

片段的相关code:

 公共类Fragment_1扩展SherlockFragment {
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
    //如果时间允许,我会努力让自定义适配器与TreeSet的实现
    TreeSet中< BlacklistWord> theSet = MainActivity.getInstance()datasource.GetAllWords()。
    ArrayList的<串GT;名单=新的ArrayList<串GT;();
    对于(BlacklistWord我:theSet){
        的System.out.println(i.getWord());
        list.add(i.getWord());
    }
    Collections.sort(名单);    //制作黑名单
    ListView控件=新的ListView(getActivity());
    listView.findViewById(R.id.listview);
    适配器=新ArrayAdapter<串GT;(getActivity(),android.R.layout.simple_list_item_1,清单);
    listView.setAdapter(适配器);
    ((BaseAdapter)listView.getAdapter())notifyDataSetChanged();
    container.addView(ListView控件);
    返回inflater.inflate(R.layout.blacklist,集装箱,FALSE);
    //返回inflater.inflate(R.layout.blacklist,集装箱,FALSE);
}
}

中的XML是

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>< ListView控件
    机器人:ID =@ + ID /列表视图
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT/>
< / LinearLayout中>


解决方案

更改code到(盲编码):

 公共类Fragment_1扩展SherlockFragment {公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
    / **
    **更改你得到的数据的方式。不要把引用这样的活动。
    ** /
    TreeSet中< BlacklistWord> theSet = MainActivity.getInstance()datasource.GetAllWords()。
    ArrayList的<串GT;名单=新的ArrayList<串GT;();
    对于(BlacklistWord我:theSet){
        的System.out.println(i.getWord());
        list.add(i.getWord());
    }
    Collections.sort(名单);
    视图V = inflater.inflate(R.layout.blacklist,集装箱,FALSE);    ListView控件= view.findViewById(R.id.listview);
    适配器=新ArrayAdapter<串GT;(getActivity(),android.R.layout.simple_list_item_1,清单);
    listView.setAdapter(适配器);
    返回视图。
}
}

您的列表视图无法显示,因为你错误地创建带有构造一个Li​​stView,那么你调用findViewById(其中没有任何用处),然后设置一个适配器,调用notify数据集变化,你到底重新返回另一个列表。

I have an application where there are several tabs; interacting with any one calls upon a fragment to be displayed. Unfortunately when I switch to the below fragment, my listView does not appear, despite the fact that the list in question is populated. Thank you very much for any help you can provide.

The fragment's relevant code:

public class Fragment_1 extends SherlockFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    //If time permits, I will try to make a Custom Adapter implemented with a TreeSet
    TreeSet<BlacklistWord> theSet =  MainActivity.getInstance().datasource.GetAllWords();
    ArrayList<String> list = new ArrayList<String>();
    for(BlacklistWord i :theSet){
        System.out.println(i.getWord());
        list.add(i.getWord());
    }
    Collections.sort(list);

    //Making BlackList 
    listView = new ListView(getActivity());
    listView.findViewById(R.id.listview);
    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,  list);
    listView.setAdapter(adapter);
    ((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
    container.addView(listView);
    return inflater.inflate(R.layout.blacklist, container, false);
    //      return inflater.inflate(R.layout.blacklist, container, false);
}
}

The XML is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

解决方案

Change the code to (blind coding):

public class Fragment_1 extends SherlockFragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    /**
    ** Change the way you get the data. Don't keep references to activities like that.
    **/
    TreeSet<BlacklistWord> theSet =  MainActivity.getInstance().datasource.GetAllWords();
    ArrayList<String> list = new ArrayList<String>();
    for(BlacklistWord i :theSet){
        System.out.println(i.getWord());
        list.add(i.getWord());
    }
    Collections.sort(list);
    View v = inflater.inflate(R.layout.blacklist, container, false);

    listView = view.findViewById(R.id.listview);
    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,  list);
    listView.setAdapter(adapter);
    return view;
}
}

Your list view is not showing because you're creating erroneously a ListView with constructor, then you're calling a findViewById (which does nothing useful), then setting an adapter, call notify data set changed and in the end you're returning another list.

这篇关于在的ListView片段不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆