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

2 つのログ メソッド 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 に答える