I have an application where there are several tabs; interacting with any one calls upon a fragment to be displayed. Unfortunately when I switch to the below fragment, my listView does not appear, despite the fact that the list in question is populated. Thank you very much for any help you can provide.
The fragment's relevant code:
public class Fragment_1 extends SherlockFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//If time permits, I will try to make a Custom Adapter implemented with a TreeSet
TreeSet<BlacklistWord> theSet = MainActivity.getInstance().datasource.GetAllWords();
ArrayList<String> list = new ArrayList<String>();
for(BlacklistWord i :theSet){
System.out.println(i.getWord());
list.add(i.getWord());
}
Collections.sort(list);
//Making BlackList
listView = new ListView(getActivity());
listView.findViewById(R.id.listview);
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
container.addView(listView);
return inflater.inflate(R.layout.blacklist, container, false);
// return inflater.inflate(R.layout.blacklist, container, false);
}
}
The XML is
<?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="vertical" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Change the code to (blind coding):
public class Fragment_1 extends SherlockFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
/**
** Change the way you get the data. Don't keep references to activities like that.
**/
TreeSet<BlacklistWord> theSet = MainActivity.getInstance().datasource.GetAllWords();
ArrayList<String> list = new ArrayList<String>();
for(BlacklistWord i :theSet){
System.out.println(i.getWord());
list.add(i.getWord());
}
Collections.sort(list);
View v = inflater.inflate(R.layout.blacklist, container, false);
listView = view.findViewById(R.id.listview);
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
return view;
}
}
Your list view is not showing because you're creating erroneously a ListView with constructor, then you're calling a findViewById (which does nothing useful), then setting an adapter, call notify data set changed and in the end you're returning another list.