'ListView in Fragment Not Displayed
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>
Solution 1:[1]
use this way:
public class LListFragment extends ListFragment {
private String[] line;
public static final String[] TITLES = { "Henry IV (1)", "Henry V",
"Henry VIII", "Richard II", "Richard III", "Merchant of Venice",
"Othello", "King Lear" };
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ListView listv = getListView();
setListAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.scan_row, R.id.textView1, TITLES));
}
}
or you have listview inside xml then..
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.llist_layout, container, false);
// do your view initialization here
listv = (ListView) view.findViewById(R.id.lineDlist);
name = (TextView) view.findViewById(R.id.lineName);
st = (TextView) view.findViewById(R.id.lineSt);
listv.setAdapter(new ImageAdapter(getActivity(),
GeneralClass.lineDetails));
return view;
}
Solution 2:[2]
Because you're creating a new ListView, and trying to find your xml listview inside that as a child...
Instead use something as;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.blacklist, container, false);
ListView listview = (ListView) view.findViewById(R.id.listview);
//Making BlackList
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
container.addView(listView);
return view;
}
Solution 3:[3]
ListView visibilty or showing ERROR...
notice that if you use fragment which extends ListFragment,
use listview defined id like list and define it just like this : android:id="@+id/listview.
and define it in your Fragment Class just like that : frgView.findViewById(android.R.id.listview);
If you still have a problem with visibility of the listview remember that change layout of fragment with <FragmentLayout>.
Would you like use in your project add it under .
Have good coding days...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dhaval Parmar |
| Solution 2 | Stefan de Bruijn |
| Solution 3 | Oussama ZAGHDOUD |
