【问题标题】:ListView in Fragment Not Displayed片段中的ListView不显示
【发布时间】:2013-06-08 05:30:34
【问题描述】:

我有一个应用程序,其中有多个选项卡;与任何人交互都会调用要显示的片段。不幸的是,当我切换到下面的片段时,我的 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>

【问题讨论】:

    标签: android android-listview android-fragments


    【解决方案1】:

    将代码改为(盲编码):

    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(没有任何用处),然后设置适配器,调用通知数据集已更改,最后您'正在返回另一个列表。

    【讨论】:

    • 您好,Gunar,感谢您的回复。当我尝试实施您的解决方案时,由于未首先在孩子的父母上调用“removeView()”而引发 RuntimeException。我应该在 MainActivity 中执行此 removeView 吗?
    • 我发布了上面的代码,假设您在父活动中没有做任何花哨的事情。从视图层次结构的角度来看,片段应该与 Activity 隔离,反之亦然……我不了解您的业务逻辑,因此无法发表更多评论。
    • 我用这行代码弄明白了: ((ViewGroup) listView.getParent()).removeView(listView);从另一个 SO 帖子推荐。
    【解决方案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;
        }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-04
        相关资源
        最近更新 更多