2011-06-24 70 views
0

我想做一個listview,每一行都包含一個imageview和兩個文本視圖。顯示圖像和第二個textview,但第一個textview是空白的......無法弄清楚有什麼問題......我無法將自己的頭包裹在自定義列表適配器中......我已閱讀http://commonsware.com/Android/excerpt.pdfListView不顯示第一個textview項

我的數據正確加載到數組列表。 我的代碼:

private class SongAdapter extends ArrayAdapter<Szam> { 

     private ArrayList<Szam> items; 

     public SongAdapter(Context context, int textViewResourceId,  ArrayList<Szam> 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.row, null); 
       } 
       Szam o = items.get(position); 
       if (o != null) { 
         TextView tt = (TextView) v.findViewById(R.id.firstLine); 
         TextView bt = (TextView) v.findViewById(R.id.secondLine); 
         if (tt != null) { 
           tt.setText(o.getArtist());       } 
         if(bt != null){ 
           bt.setText(o.getTitle()); 
         } 
       } 
       return v; 
     } 

我的XML佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent"  android:minHeight="?android:attr/listPreferredItemHeight" 
android:layout_height="wrap_content" android:padding="6dip"> 

<TextView android:id="@+id/secondLine" android:layout_width="fill_parent" 
    android:layout_height="26dip" android:layout_toRightOf="@+id/icon" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" android:singleLine="true" 
    android:ellipsize="marquee" 
    android:text="Simple application that shows how to use RelativeLayout" /> 

<TextView android:id="@+id/firstLine" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:layout_toRightOf="@+id/icon" 
    android:layout_alignParentRight="true" android:layout_alignParentTop="true" 
    android:layout_above="@id/secondLine" 
    android:layout_alignWithParentIfMissing="true" android:gravity="center_vertical" 
    android:text="My Application" /> 
<ImageView android:layout_marginRight="6dip" android:id="@+id/icon" 
    android:src="@drawable/play" android:layout_width="wrap_content" 
    android:layout_height="fill_parent" android:layout_alignParentLeft="true"></ImageView> 

</RelativeLayout> 

修正的佈局是:

<TextView android:id="@+id/secondLine1" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:layout_toRightOf="@+id/icon" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" android:singleLine="true" 
    android:ellipsize="marquee" 
    android:textColor="#ffffff" 

    android:textSize="12sp" 
    android:text="Simple application that shows how to use RelativeLayout" /> 

<TextView android:id="@+id/firstLine1" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:layout_toRightOf="@+id/icon" 
    android:layout_alignParentRight="true" android:layout_alignParentTop="true" 
    android:ellipsize="marquee" 
    android:textColor="#ffffff" 

    android:textSize="16sp" 
    android:layout_alignWithParentIfMissing="true" 
    android:text="My Application" /> 

<ImageView android:layout_marginRight="6dip" android:id="@+id/icon" 
    android:src="@drawable/play" android:layout_width="wrap_content" 
    android:layout_height="fill_parent" android:layout_alignParentLeft="true"></ImageView> 

回答

1

不能使用LinearLayo UTS?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" 
    android:minHeight="?android:attr/listPreferredItemHeight" android:layout_height="wrap_content" android:padding="6dip"> 
    <ImageView android:layout_marginRight="6dip" android:id="@+id/icon" android:src="@drawable/play" android:layout_width="wrap_content" 
     android:layout_height="fill_parent"></ImageView> 
    <LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:orientation="vertical" 
     android:layout_height="wrap_content"> 
     <TextView android:text="My Application" android:layout_height="wrap_content" android:gravity="center_vertical" android:id="@+id/firstLine" 
      android:layout_width="fill_parent"></TextView> 
     <TextView android:singleLine="true" android:text="Simple application that shows how to use RelativeLayout" android:layout_height="wrap_content" 
      android:ellipsize="marquee" android:id="@+id/secondLine" android:layout_width="fill_parent"></TextView> 
    </LinearLayout> 
</LinearLayout> 
+0

我重寫了我的行佈局到你給我的代碼,現在我的應用程序在調用v = vi.inflate(R.layout.row,null)時給了我一個錯誤; –

+0

我已經改變了佈局來匹配你的參數。請看看 –

+0

我已經弄明白了你的代碼的幫助......謝謝:) –