У меня есть 4 поля в настраиваемом макете строк для списка. У меня есть 4 поля в моем массиве, я хочу, чтобы отобразить в этих 4-х полей на ListView:Заполнение ListView элементами из массива в Java
- cat_ID_PK
- cat_name
- cat_amount
- is_recurring
Я не знаю, как правильно это сделать. Вот то, что я до сих пор:
private void loadListView(Expenses[] mExpenseArray) {
//This code will write the "name" variable correctly to the logcat, so I know I'm
getting the right values in my array.
String name = mExpenseArray[0].getCatName();
Log.v("log_tag", name);
ArrayAdapter<Expenses> adapter = new ArrayAdapter<Expenses>(getApplicationContext(), R.layout.list_row, R.id.catName, mExpenseArray);
final ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
}
Этот код заполнит только R.id.catName
, но когда она заполнит его, это выглядит следующим образом: [email protected]
(или что-то подобное). Ни одно из других полей не заполнено вообще, что, как я предполагаю, связано с 3-м параметром в моем массиве ArrayAdapter R.id.catName
. Однако, если я возьму этот параметр, что я получаю эту ошибку:
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
Вот код для моего пользовательского макета строки:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<TextView
android:id="@+id/catName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_centerVertical="true"
android:paddingBottom ="10dip"
android:text="@string/catName"
android:textColor="#040404"
android:textSize="25dip"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/catAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="27dip"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:paddingBottom ="10dip"
android:text="$45.00"
android:textColor="#040404"
android:textSize="25dip"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/catType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/catName"
android:layout_alignLeft="@+id/catName"
android:paddingTop="5dip"
android:layout_centerHorizontal="true"
android:text="@string/catType"
android:textColor="#343434"
android:textSize="15dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="catID"
android:id="@+id/catID"
android:layout_alignBottom="@+id/catType"
android:layout_toRightOf="@+id/catType"
android:layout_toEndOf="@+id/catType"
android:layout_marginLeft="27dp"
android:visibility="invisible" />
</RelativeLayout>
Как правильно отображающие мои 4 поля массива в моей ListView?
Вы должны использовать пользовательский адаптер – Abdellah
попробуйте использовать пользовательский адаптер. –
«Расходы» должны быть моделью данных, поэтому вам нужно использовать пользовательский адаптер, чтобы получить значение от модели. –