I am trying to populate a ListView with data from a web service using Retrofit, but my ListView is somehow always empty. I am getting the data from the web service fine, it just isn't getting into the ListView. setListAdapter() doesn't seem to be doing anything because the listview's count is 0 after I call it. Why is my ListView empty? Here is some of my code:
public class PopularFragment extends ListFragment{
private final String NAME = "Popular";
RestClient restClient;
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
restClient = RestClientService.getService();
restClient.getApps(new Callback<List<App>>() {
@Override
public void success(List<App> apps, Response response) {
Log.v("test", apps.toString());
setListAdapter(new AppListAdapter(getActivity(), apps));
Log.v("test", Integer.toString(getListAdapter().getCount()));
Log.v("test", Integer.toString(getListView().getCount()));
}
@Override
public void failure(RetrofitError retrofitError) {
Log.v("test", "failure");
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_browse_apps, container, false);
return view;
}
The two log methods Log.v("test", Integer.toString(getListAdapter().getCount())); and Log.v("test", Integer.toString(getListView().getCount())); both return 0.
Here is my custom adapter:
public class AppListAdapter extends ArrayAdapter<App>
{
private final Context context;
private List<App> apps;
public AppListAdapter(Context context, List<App> apps)
{
super(context, R.layout.app_list_item);
this.context = context;
this.apps = apps;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
view = inflater.inflate(R.layout.app_list_item, parent, false);
}
App app = this.apps.get(position);
TextView appName = (TextView) view.findViewById(R.id.appName);
appName.setText("TEST");
return view;
}
}
app_list_item.xml:
<?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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="App Name"
android:id="@+id/appName" />
fragment_browse_apps.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.android.devon.appfrenzy.BrowseAppsActivity$PlaceholderFragment">
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HELLO" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/list"
android:layout_centerHorizontal="true" />
apps
look like? Are you sure it's populated? – Waac