2011-05-05 24 views
0

即時嘗試顯示3個TextViews作爲ListView中的自定義行設計。不幸的是,只有2個TextView(row_title和row_postcode_city)可見...自定義ListView顯示3個垂直TextViews的問題

也許有人有一個想法?

問候, 浮動

我ArrayAdapter的代碼:

<?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> 
+0

無論你聲明layout_height,首先使它wrap_content,然後檢查 – 2011-05-05 13:17:10

+0

@Chirag,我已經修改了XML,但沒有任何改變。 row_subtitle TextView仍然不可見。 – float 2011-05-05 13:20:17

回答

1

嘗試使用下面的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> 

檢查它現在。

+0

謝謝,我發現了這個問題。在ArrayAdapter中,我使用row_postcode_city的值覆蓋了row_subtitle ... – float 2011-05-05 13:38:50

1

更改您的LinearLayout爲 「WRAP_CONTENT」 上layout_height

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

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

另外,我不知道你爲什麼在LinearLayout中有一個LinearLayout。由於這個XML只適用於單獨的行。每行將是一個水平的LinearLayout,第二個是不必要的,因爲ListView會自動垂直列出行。

最後,根LinearLayout應該使用minHeight作爲默認項目高度,而不是layout_height。

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

這應該是你的XML文件(除非你真的需要第二次的LinearLayout一些奇怪的原因)

<?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> 
+0

現在有一個名爲layout_minHeight的屬性。我有2 LinearLayouts,因爲有一個ImageView旁邊的第二個LinearLayout – float 2011-05-05 13:35:42

+0

抱歉,我的意思是minHeight – Spidy 2011-05-05 13:37:13

+0

感謝您的這個提示。但屬性android:orientation =「vertical」仍然需要:-) – float 2011-05-05 13:50:24

1

對不起,offtopic,但我聽到我的一些有用的提示 你有不必要的很多(與平出錯誤的)東西會在這裏:

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

你應該看到每一個位置,如果item retuns null意味着你做錯了什麼。在你的情況下,如果你得到空值,你只需返回重用的舊的或全新的行,這會通過顯示舊數據或根本沒有數據來混淆用戶。

if (title != null) { 

你必須讓你的行視圖對象,通過虛報,或從回收(convertView)得到它的2種方式。回收商將總是返回正確類型的視圖,所以這些檢查是無用的,因爲您的組件將始終被發現。

+0

感謝您使用這些提示。 – float 2011-05-05 13:42:42