2014-02-03 4 views
2

Я пытаюсь заполнить ListView данными из веб-службы, используя Retrofit, но мой ListView как-то всегда пуст. Я получаю данные от веб-сервиса отлично, он просто не попадает в ListView. setListAdapter(), похоже, ничего не делает, потому что счет listview равен 0 после его вызова. Почему мой ListView пуст? Вот некоторые из моего кода:Почему мой ListView пуст после вызова setListAdapter внутри ListFragment?

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 ("тест", 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

Что 'приложения 'выглядят? Вы уверены, что он заселен? – PearsonArtPhoto

+0

да Я уверен, что он заселен – theDazzler

ответ

2

Вы должны вызвать переменную конструктор 3 для ListAdapter:

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

Это сработало отлично, спасибо – theDazzler

Смежные вопросы