ListView添加Subitem

时间:2014-02-01 20:26:23

标签: android listview subitem

请帮助我,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);

1 个答案:

答案 0 :(得分: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);