2

我正在尝试使用Retrofit使用 Web 服务中的数据填充 ListView ,但我的 ListView 总是为空。我从 Web 服务中获取数据很好,只是没有进入 ListView。setListAdapter() 似乎没有做任何事情,因为我调用它后列表视图的计数为 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" />

4

1 回答 1

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;
}
于 2014-02-03T03:10:08.137 回答