简体   繁体   中英

Android - Multiple textviews in Listview

I am populating a Listview (list.xml) with a textview (list_row.xml).

I need to add an additional textview to list_row.xml, which means I need to wrap them in a layout. However, I get a Textview ID error when I do this.

list_row.xml I need:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView android:id="@+id/nameTV"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#FFFF00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>

Main.xml

public View GetView(int inPosition, View inConvertView, ViewGroup inParent) 
{
    View _row = inConvertView;

    if (_row == null) 
    {
        // Inflate Row
        Log.d(TAG, "Starting XML Row Inflation ... ");
        LayoutInflater inflater = (LayoutInflater) 
                cContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        _row = inflater.inflate(R.layout.list_row, inParent, false);
        Log.d(TAG, "Successfully completed XML Row Inflation!");
    }

    // Get item
    PropertiesAd _adProperty = GetItem(inPosition);

    // Get reference to TextView - country_name
    cAdName = (TextView) _row.findViewById(R.id.nameTV);

    //Set name
    cAdName.setText(_adProperty.toString());

    return _row;
}

For the sake of testing, I haven't added the second textview yet. I'm just trying to get this to work with the layout wrapper.

Everything works fine without the LinearLayout. But again, I will need it to add a second textview. Suggestions?

Thanks

ArrayAdapter:

public class ArrayAdapterAd extends ArrayAdapter<PropertiesAd> 
{

private static final String TAG = "AdArrayAdapter";
private Context cContext;
private TextView cAdName;
private List<PropertiesAd> cAdList = new ArrayList<PropertiesAd>();

public ArrayAdapterAd(Context inContext, int inTextViewResourceId, List<PropertiesAd> inObjects) 
{
    super(inContext, inTextViewResourceId, inObjects);

    this.cContext = inContext; 
    this.cAdList = inObjects;
}


public int GetCount() 
{
    return this.cAdList.size();
}


public PropertiesAd GetItem(int inIndex) 
{
    return this.cAdList.get(inIndex);
}


public View GetView(int inPosition, View inConvertView, ViewGroup inParent) 
{
    View _row = inConvertView;

    if (_row == null) 
    {
        // Inflate Row
        Log.d(TAG, "Starting XML Row Inflation ... ");
        LayoutInflater inflater = (LayoutInflater) 
                cContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        _row = inflater.inflate(R.layout.ad_list, inParent, false);
        Log.d(TAG, "Successfully completed XML Row Inflation!");
    }

    // Get item
    PropertiesAd _adProperty = GetItem(inPosition);

    // Get reference to TextView - country_name
    cAdName = (TextView) _row.findViewById(R.id.ad_listTextView);

    //Set country name
    cAdName.setText(_adProperty.toString());

    return _row;
}

}

Here is xml for a listview "list_row" with 2 textviews

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal">

    <TextView android:id="@+id/text1" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

    <TextView android:id="@+id/text2" android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</LinearLayout>

I'm assuming the getView() is from an ArrayAdapter subclass? If so, what does your constructor look like? Perhaps you're passing in an id reference in stead of a layout reference? This will probably resolve fine if you're whole layout is nothing more than a single TextView, but could explain why an error occurs when wrapping it in a layout.

Also, don't forget to implement the rowwrapper/viewholder pattern to improve performance and recycle views. For an example, have a look here .

By the way, you may even suffice with one of the Android built-in layouts. There a two line list item layout defined in android.R.layout.simple_list_item_2 . It may not suit your needs though, as the styling on the two elements is different. A custom layout is always the most flexible in the end. :)

Now I see your problem... you cannot do that extending the ArrayAdapter class. Instead, extend BaseAdapter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM