Android - Listview In Fragment Does Not Showing Up
Solution 1:
There are a few things that raise red flags in your code. Which leads me to my first question: are you using eclipse or intellij? Because it doesn't look like it'll even compile to me. Maybe you edited this on here, but now to help you get it working. Don't even use onActivityCreated
for initializing your ListView
in a Fragment (Andrew also correctly brought up the point that you aren't even using it correctly, which is why I am surprised its even compiling if it is). You should be doing it in onCreateView
. Now I will post your code that I have edited. I am not testing it so it serves as a better guide to what it should look like (and hopefully it compiles).
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewrootView= inflater.inflate(R.layout.event, container, false);
ArrayList<HashMap<String, String>> eventList = controller
.getAllEvents();
if (eventList.size() != 0) {
ListViewlv= (ListView) rootView.findViewById(R.id.list);
lv.setOnItemClickListener(newOnItemClickListener() {
@OverridepublicvoidonItemClick(AdapterView<?> parent, View view,
int position, long id) {
eventId = (TextView) view.findViewById(R.id.eventId);
StringvalEventId= eventId.getText().toString();
IntentobjIndent=newIntent(getActivity(),
EditEvent.class);
objIndent.putExtra("eventId", valEventId);
startActivity(objIndent);
}
});
ListAdapteradapter=newSimpleAdapter(getActivity(), eventList,
R.layout.view_event_entry, newString[] { "eventId",
"eventName" }, newint[] { R.id.eventId,
R.id.eventName });
lv.setAdapter(adapter);
}
show = (Button)rootView.findViewById(R.id.button1);
show.setOnClickListener(this);
return rootView;
}
...
Solution 2:
There are a few things wrong with this. First off, you overrode onActivityCreated()
incorrectly. Instead of what you have, it should have the following header:
publicvoidonActivityCreated(Bundle savedInstanceStates)
Because you use the wrong header, it is never called.
Secondly, you may get some errors from using rootView
in your onActivityCreated()
method because it is not declared in that method. Consider calling getView()
instead, as that returns the rootView for you.
Post a Comment for "Android - Listview In Fragment Does Not Showing Up"