Reputation: 25
I have spent too long to populate a listView in a fragment.
I don't want a list fragment or have it be populated by a DB, just want it populated manually also if you could show how to make it so that you can scroll that would be helpful.
The "rootView.findViewById" comes up with the error "Cannot resolve symbol 'rootView'"
here is my code:
public class Favourites extends Fragment {
@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;
}
}
and 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" />
Please help and put it in plain simple English I am new to this. Thank you.
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)
Upvotes: 0
Views: 2915
Reputation: 1789
In activity you cannot use this
as your context
. You need to get activity your fragment belongs to as your context with getActivity()
.
Next, you cannot perform findViewById()
operations before you actually have the view. The view is created in onCreateView
, but you are calling it in onCreate
already. Everything view related must be performed on view you get from onCreateView
. To find your ListView
you'll have to move it there and call it
ListView listView = (ListView) rootView.findViewById(R.id.favefoodslist);
EDIT
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(fragment_favourites, container, false);
String[] favouritefoods = {"@@@", "!!!!", "...", "..."};
//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;
}
Upvotes: 1
Reputation: 26
If you are using static data, you should do it in onCreateView method. If you need to repopulate it when you load data, for example, you should initialize the adapter with an empty ArrayList. When you got the data, copy it to the empty ArrayList and call adapter.notifyDatasetChanged().
Upvotes: 0