未显示片段中的ListView

时间:2013-06-12 07:21:56

标签: android android-listview android-fragments

我有一个应用程序,其中有几个选项卡;与任何一个进行交互都会调用要显示的片段。不幸的是,当我切换到下面的片段时,我的listView没有出现,尽管有问题的列表被填充。非常感谢您提供的任何帮助。

片段的相关代码:

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>

3 个答案:

答案 0 :(得分:2)

使用这种方式:

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中有listview然后..

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

答案 1 :(得分:2)

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

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(它没有任何用处),然后设置适配器,调用通知数据集已更改,最后是您'重新回到另一个名单。

答案 2 :(得分:1)

因为您正在创建一个新的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;
}