Reputation: 886
I'm trying to populate a ListView using JSON but I'm running into trouble referencing the ListView.
Here is my layout_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No data" />
</LinearLayout>
I read on the ListFragment documentation that when designing a custom list, I need to keep the ListView's id as "@id/android:list".
Here is the piece of code that I am having problems with:
list = (ListView)getView().findViewById(R.id.list);
I currently have that code in my JSON parsing method but I have tried putting it in my public View onCreateView method according to other suggestions that I read online, but I still get the following error:
Cannot Resolve symbol 'list'
Would anyone be able to help clear up my issue?
Upvotes: 1
Views: 105
Reputation: 17439
The class ListFragment
includes a listview and adapter inside it, and also provides some methods that allow you to get the listview and operate on it.
You can refer to it (from the ListFragment) in this way:
getListView()
You need to refer to the android:id="@id/android:list"
only if you want customize the layout. In this case you need to override the onCreateView
method, and then refer to the list element "@android:id/list"
(ListView)getView().findViewById(android.R.id.list)
and
getListView()
are the same things. But you don't need to use the first expression, except if you want customize the ListView
inside the ListFragment
Upvotes: 1
Reputation: 481
list = (ListView)getView().findViewById(R.id.list);
do you use your application R or android's R? try to code like this:
list = (ListView)getView().findViewById(android.R.id.list);
Upvotes: 1