你如何在片段中制作和填充listView?

时间:2015-08-15 12:51:37

标签: android android-fragments

我花了太长时间在片段中填充listView。

我不想要列表片段或让它由数据库填充,只是想要手动填充它,如果你可以展示如何制作它以便你可以滚动这将是有帮助的。

" rootView.findViewById"出现错误"无法解析符号' rootView'"

这是我的代码:

公共类收藏夹扩展片段{

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

    View rootView = inflater.inflate(fragment_favourites, container, false);

    String[] favouritefoods = {"Lasagne", "cheese Burrito", "jalepano bread", "chedder cheese"};

    //Build adapter
    ArrayAdapter<String> favefoodadapter = new ArrayAdapter<String>(
            getActivity(), //some context for the activity
            R.layout.favelist, //layout to be displayed(create)
            favouritefoods); //strings to be diplayed



    ListView listView = (ListView) rootView.findViewById(R.id.favefoodslist);
    listView.setAdapter(favefoodadapter);


    return rootView;
}

}

和fragments_favourites.xml:

<RelativeLayout
    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="activity.Favourites"
android:id="@+id/favefoodfragment">

<TextView
    android:id="@+id/favourites_textview"
    android:layout_alignParentTop="true"
    android:layout_marginTop="100dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:textSize="45dp"
    android:text="favourites"
    android:textStyle="bold" />

<TextView
    android:layout_below="@id/favourites_textview"
    android:layout_centerInParent="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="12dp"
    android:layout_marginTop="10dp"
    android:gravity="center_horizontal"
    android:text="Edit fragment_home.xml to change the appearance"
    android:id="@+id/textView3" />

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/favefoodslist"
    android:layout_below="@+id/textView3"
    android:layout_centerHorizontal="true" />

请帮助并用简单的英语说明我是新手。谢谢。

logcat的:

08-18 13:24:57.100  31954-31954/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.nrnazz747123.navigationapp, PID: 31954
    java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.ListView
            at activity.Favourites.onCreateView(Favourites.java:40)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456)
            at android.os.Handler.handleCallback(Handler.java:808)
            at android.os.Handler.dispatchMessage(Handler.java:103)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5345)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
            at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案