开发者

problem with custom ListView showing 3 vertical TextViews

im trying to display 3 TextViews as a custom ro开发者_StackOverfloww design in a ListView. Unfortunatly, only 2 TextViews ( row_title and row_postcode_city) are visible...

Maybe somebody has an idea?

Regards, float

Code of my ArrayAdapter:

private class PartnerAdapter extends ArrayAdapter<Partner> {

        private ArrayList<Partner> items;

        public PartnerAdapter(Context context, int textViewResourceId, ArrayList<Partner> items) {
                super(context, textViewResourceId, items);
                this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.partner_suche_ergebnis_row, parent, false);
                }
                Partner o = items.get(position);
                if (o != null) {
                        TextView title = (TextView) v.findViewById(R.id.row_title);
                        TextView subtitle = (TextView) v.findViewById(R.id.row_subtitle);
                        TextView postcode_city = (TextView) v.findViewById(R.id.row_postcode_city);

                        if (title != null) {
                            title.setText(o.getTitle());
                        }
                        if(subtitle != null){
                            subtitle.setText(o.getSubtitle());
                        }
                        if(postcode_city != null){
                            subtitle.setText(o.getPostcode() + " " + o.getCity());
                        }
                }
                return v;
        }
}

XML of the custom row design:

<?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="?android:attr/listPreferredItemHeight"
android:padding="0dip"><!--
<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="6dip"
    android:src="@drawable/icon" />
--><LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_weight="1"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/row_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="bold"/>
    <TextView
        android:id="@+id/row_subtitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

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

    />
</LinearLayout>
</LinearLayout>


Try to use the following xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="0dip">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_weight="1"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/row_title"
        android:text="1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="bold"/>
    <TextView
        android:id="@+id/row_subtitle"
        android:text="2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

    />
    <TextView
    android:text="3"
        android:id="@+id/row_postcode_city"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

    />
</LinearLayout>
</LinearLayout>

Check it now.


Change your LinearLayout to "wrap_content" on layout_height

<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_weight="1"
    android:layout_height="wrap_content">

Also, I don't know why you have a LinearLayout inside a LinearLayout. Since this xml is only for individual rows. Each row will be a horizontal LinearLayout, the second one being unnecessary as ListView automatically lists rows vertically.

Finally, the root LinearLayout should use minHeight for the default Item Height, not layout_height.

android:minHeight="?android:attr/listPreferredItemHeight"

This should be your xml file (unless you really need the second LinearLayout for some odd reason)

<?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:minHeight="?android:attr/listPreferredItemHeight"
android:padding="0dip">
    <TextView
        android:id="@+id/row_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="bold"/>
    <TextView
        android:id="@+id/row_subtitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />
    <TextView
        android:id="@+id/row_postcode_city"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />
</LinearLayout>


Sorry for offtopic, but I here some useful tips from me You have a LOT of needless (and flat out wrong) stuff going on here:

Partner o = items.get(position);
            if (o != null) {

You should get something for every position, if item retuns null that means you are doing something wrong. In your case, if you get null you just return reused old, or brand new row, which will confuse the user by displaying the old data, or no data at all.

if (title != null) {

You have 2 ways of getting View object of your row, by inflating, or getting it from recycler (convertView). And recycler will ALLWAYS return the right type of View, so those check are useless since your components will always be found.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜