为什么在ListFragment中调用setListAdapter后我的ListView为空?

时间:2014-02-03 03:03:31

标签: android android-listview retrofit

我正在尝试使用Retrofit使用来自Web服务的数据填充ListView,但我的ListView总是空的。我从Web服务获取数据很好,它只是没有进入ListView。 setListAdapter()似乎没有做任何事情,因为我调用它后listview的计数为0。为什么我的ListView为空?以下是我的一些代码:

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;
}

两个日志方法Log.v(“test”,Integer.toString(getListAdapter()。getCount()));和             Log.v(“test”,Integer.toString(getListView()。getCount()));两者都返回0。

这是我的自定义适配器:

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" />

1 个答案:

答案 0 :(得分:2)

您应该为ListAdapter调用3变量构造函数:

public AppListAdapter(Context context, List<App> apps)
{
    super(context, R.layout.app_list_item,apps);
    this.context = context;
}

然后您实际上不需要关闭应用程序,可以使用getItem

检索它
@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 = getItem(position);

    TextView appName = (TextView) view.findViewById(R.id.appName);
    appName.setText("TEST");

    return view;
}