【问题标题】:Fragment not loading片段未加载
【发布时间】:2015-03-13 17:48:35
【问题描述】:

我正在尝试在我的应用程序的两个选项卡之一中使用 listview 填充片段。

但是,得到的是这一切:(这会一直持续下去......)

这就是我想要做的。

ListFragment.Java

public class ListFragment extends Fragment {

  Other methods 
   ...
  //some code

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment


        java.util.List<HashMap<String, String>> aList = new ArrayList<HashMap<String,String>>();

        for(int i=0;i<len;i++){
            HashMap<String, String> hm = new HashMap<String,String>();
            hm.put("tit", "Title : " + title[i]);
            hm.put("init","Initial Price : " + cur_price[i]);
            hm.put("cur","Current Price  : " + init_price[i]);
            hm.put("img", d[i].toString());
            aList.add(hm);
        }



        // Keys used in Hashmap
        String[] from = { "tit","init","cur","img"};

        // Ids of views in listview_layout
        int[] to = { R.id.tit,R.id.init,R.id.cur,R.id.img};

        // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(context, aList, R.layout.listview_layout, from, to);

        // Getting a reference to listview of fragment_list_items.xml layout file
        ListView listView = ( ListView ) view.findViewById(R.id.listview);

        // Setting the adapter to the listView
        listView.setAdapter(adapter);

        return inflater.inflate(R.layout.fragment_list_items, container, false);

    }

请问哪里有问题?

编辑:当我没有选项卡但只有一个活动要显示时,相同的代码运行良好。

我的布局文件:

fragment_list_items.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.example.nikhil.amazon1.ListFragment">

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

</FrameLayout>

ListView.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="horizontal"
    >

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription=""
        android:paddingTop="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="10dp"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/tit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            />

        <TextView
            android:id="@+id/init"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            />

        <TextView
            android:id="@+id/cur"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            />
    </LinearLayout>
</LinearLayout>

【问题讨论】:

    标签: android android-fragments android-listview simpleadapter pagerslidingtabstrip


    【解决方案1】:

    首先你必须通过inflater创建视图,因为你不能从中findItemById。 并重命名你的班级。 ListFragment -> 其他名称。

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
    
        View view = inflater.inflate(R.layout.fragment_list_items, container, false)
    
        java.util.List<HashMap<String, String>> aList = new ArrayList<HashMap<String,String>>();
    
        for(int i=0;i<len;i++){
            HashMap<String, String> hm = new HashMap<String,String>();
            hm.put("tit", "Title : " + title[i]);
            hm.put("init","Initial Price : " + cur_price[i]);
            hm.put("cur","Current Price  : " + init_price[i]);
            hm.put("img", d[i].toString());
            aList.add(hm);
        }
    
    
    
        // Keys used in Hashmap
        String[] from = { "tit","init","cur","img"};
    
        // Ids of views in listview_layout
        int[] to = { R.id.tit,R.id.init,R.id.cur,R.id.img};
    
        // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(context, aList, R.layout.listview_layout, from, to);
    
        // Getting a reference to listview of main.xml layout file
        ListView listView = ( ListView ) view.findViewById(R.id.listview);
    
        // Setting the adapter to the listView
        listView.setAdapter(adapter);
    
        return view;
    
    }
    

    【讨论】:

    • 片段中是否只有列表视图?
    • 是的,我只有列表视图。我将使用我的 XML 布局文件更新问题。有什么帮助吗? :)
    • 似乎很有趣的错误)您将您的类命名为 ListFragment,它已经在 android SDK 中定义。因此,当您在 tabView(example) 中设置片段时,例如 // return new ListFragment(); - 您只需从 DEFINED ListFragment 中明确对象。尝试重命名您的班级;发布结果
    • 将文本视图等可见项目添加到您的 R.layout.listview_layout xml 以检查我的版本。
    • 感谢@Max Vitruk。有效。我更改了班级名称。此外,我遇到了上下文问题,我已修复。谢谢你的时间。 :)
    【解决方案2】:

    我也有这个问题。您可能需要一个片段元素而不是框架布局。 您可以尝试在 fragment_list_items.xml 中使用

    【讨论】:

      猜你喜欢
      • 2012-11-25
      • 2018-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多