android-fragments 未显示片段中的ListView

kyxcudwk  于 2022-11-14  发布在  Android
关注(0)|答案(4)|浏览(206)

我有一个应用程序,其中有几个选项卡;与任何一个人交互都需要显示一个片段。2很遗憾,当我切换到下面的片段时,我的listView没有出现,尽管事实上有问题的列表已经填充。3非常感谢你能提供的任何帮助。
片段的相关代码:

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

XML是

<?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>
ibps3vxo

ibps3vxo1#

将代码更改为(盲编码):

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

您的列表视图没有显示,因为您错误地使用构造函数创建了一个ListView,然后您调用了一个findViewById(它没有任何用处),然后设置了一个适配器,调用了notify data set changed,最后您返回了另一个列表。

u2nhd7ah

u2nhd7ah2#

使用以下方式:

public class LListFragment extends ListFragment {

    private String[] line;

    public static final String[] TITLES = { "Henry IV (1)", "Henry V",
            "Henry VIII", "Richard II", "Richard III", "Merchant of Venice",
            "Othello", "King Lear" };

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


        ListView listv = getListView();

        setListAdapter(new ArrayAdapter<String>(getActivity(),
                R.layout.scan_row, R.id.textView1, TITLES));
    }
}

或者您在xml中有列表视图,那么..

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.llist_layout, container, false);
        // do your view initialization here
        listv = (ListView) view.findViewById(R.id.lineDlist);
        name = (TextView) view.findViewById(R.id.lineName);
        st = (TextView) view.findViewById(R.id.lineSt);

        listv.setAdapter(new ImageAdapter(getActivity(),
                GeneralClass.lineDetails));

        return view;
    }
izj3ouym

izj3ouym3#

因为您正在创建一个新的ListView,并试图在其中找到您的xml listview作为子视图...
而不是用某物作为;

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

    View view = inflater.inflate(R.layout.blacklist, container, false);

    ListView listview = (ListView) view.findViewById(R.id.listview);

    //Making BlackList 
    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,  list);
    listView.setAdapter(adapter);
    ((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
    container.addView(listView);

    return view;
}
31moq8wy

31moq8wy4#

ListView visibilty或正在显示ERROR...请注意,如果您使用扩展ListFragment的片段,请使用listview定义的id(如 list),并按如下方式定义它:android:id="@+id/listview .
并在Fragment类中定义它,如下所示:frgView.findViewById(android.R.id.listview);
如果你仍然有一个问题的可见性的列表视图记住,改变布局的片段与<FragmentLayout>
是否要在项目中使用将其添加到下。
祝您有一个愉快的编码日...

相关问题