2013-05-26 2 views
2

Я следил за несколькими учебниками, но я до сих пор не могу заполнить свой список. Что я делаю неправильно?ListView с пользовательским адаптером

это расположение spaced_list.xml

<?xml version="1.0" encoding="utf-8"?> 
<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mList" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

</ListView> 

это spaced_list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:padding="10dp" 
    android:background="#efefef"> 

<TextView 
    android:id="@+id/leftItem" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:gravity="left" 
    android:text="Left Text View" /> 

<TextView 
    android:id="@+id/rightItem" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:gravity="right" 
    android:text="Right Text View" /> 

</RelativeLayout> 

и это класс

И я убедился, что я получение значений от cDataSource.getAllCategories();

+1

Какая ошибка у вас? – Daniel

+0

Вы можете проверить ответ, который я поставил ниже. Также у вас есть лишние атрибуты в _spaced_list_item.xml_, потому что это «RelativeLayout», вы можете удалить атрибуты «android: orientation» и «android: gravity» – jpintado

ответ

1

Изменить это:

public CategoriesAdapter(Context context, ArrayList<Category> categories){ 
     super(context, 0); // <- Wrong constructor 
     this.context = context; 
     this.categories = categories; 
    } 

Для этого:

public CategoriesAdapter(Context context, ArrayList<Category> categories){ 
     super(context, 0, categories); 
     this.context = context; 
     this.categories = categories; 
    } 

Вы в настоящее время вызова this конструктор суперкласса:

public ArrayAdapter(Context context, int textViewResourceId) { 
     init(context, textViewResourceId, 0, new ArrayList<T>()); 
    } 

который в конечном итоге создать новую ArrayList и игнорировать данные, которые вы передали.

+0

Работает как очарование! Спасибо. –

0

Проблема заключается в конструкторе вашего адаптера. Вам необходимо предоставить ArrayList<Category>, если вы звоните super(). Вот код:

public CategoriesAdapter(Context context, ArrayList<Category> categories){ 
    super(context, 0, categories); //Providing objects to represent in the ListView 
    this.context = context; 
    this.categories = categories; 
} 

Это документация со ссылкой на эту ArrayAdapter constructor.

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