I’m porting my app from AsyncTasks to Fragements.
But how can I access the listView (id: list) element within my fragment?
class MyFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.list_fragment, container, false);
ListView listView = getListView(); //EX:
listView.setTextFilterEnabled(true);
registerForContextMenu(listView);
return v;
}
}
xml:
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
Ex:
Caused by: java.lang.IllegalStateException: Content view not yet created
as the
onCreateViewdoc stays:so since the method does not return, you will are not able to access the
ListViewthroughgetListView(). You can obtain a valid reference in theonActivityCreatedcallback.Or you can try using
v.findViewById(android.R.id.list)if theListViewis declared insidelist_fragment.xml