سؤال

Help me please, how I can in my case in ListView add Subitem?

I have 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>

In Subitem I need to add a few lines from the database (HOME_COLUMN, CITY_COLUMN, STREET_COLUMN, and so on). I got only add an item (R.id.label) only one row from the database (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);
هل كانت مفيدة؟

المحلول

Just use a custom ArrayAdapter:

1) Define your custom ArrayAdapter. Fill in the body of getView() to create a view based on each item your pass to the adapter;

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) Pass the adapter to your ListView, and add data.

YourArrayAdapter<RSSItem> adapter = new YourArrayAdapter<RSSItem>(this);
adapter.addAll(myRssFeed.getList());
setListAdapter(adapter);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top