-1

请帮助我,在我的情况下如何ListView添加Subitem

我有list_row.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="wrap_content"
    android:orientation="horizontal"
    android:background="@color/back_black" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="30dp"
        android:layout_height="40dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:src="@drawable/ic_launcher" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp"
        android:text=""
        android:textColor="@color/yellow"
        android:textSize="20sp" >
    </TextView>

    <TextView
        android:id="@+id/sublabel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp"
        android:text=""
        android:textColor="@color/white"
        android:textSize="12sp" />

</LinearLayout>

我需要从Subitem数据库中添加几行(HOME_COLUMNCITY_COLUMNSTREET_COLUMN等)。我只R.id.label从数据库中添加了一个项目()只有一行(NAME_COLUMN

private void fillData() {

        String[] from = new String[] { KvartDB.NAME_COLUMN };
        int[] to = new int[] { R.id.label };


        //how to add R.id.sublabel a few lines from the database ?


        adapter = new SimpleCursorAdapter(this, R.layout.list_row, null, from,
                to, 0);
        setListAdapter(adapter);
4

1 回答 1

0

只需使用自定义 ArrayAdapter:

1) 定义您的自定义 ArrayAdapter。填写 getView() 的主体以根据您传递给适配器的每个项目创建一个视图;

public class YourArrayAdapter<YourDataObject> extends ArrayAdapter<T> {
public YourArrayAdapter(Context context) {
    super(context, 0); // Pass in 0 because we will be overriding getView()
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // getView() gets called when this item becomes visible in the ListView
    // All you have to do is build a view with your data object and return it.
    YourDataObject yourDataObject = getItem(position);
    YourView view = new YourView(yourDataObject);

}

2) 将适配器传递给您的 ListView,并添加数据。

YourArrayAdapter<RSSItem> adapter = new YourArrayAdapter<RSSItem>(this);
adapter.addAll(myRssFeed.getList());
setListAdapter(adapter);
于 2014-02-01T20:40:21.810 回答