2012-12-23 16 views
0

我是android中的新手。在Android中的ListAdapter中輸入一個對象

我必須這樣被視爲一個列表: 圖片名稱DESC

我有一個包含所有以上3信息桶的列表。 問題是,我只能讓一個字段出現在視圖中。在這種情況下,bucket.getBucketName()。

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     List<Bucket> buckets = BucketHandler.getBucketList(); 
     List<String> bucketList = new ArrayList<String>(); 
     for (Bucket bucket : buckets){ 
      bucketList.add(bucket.getBucketName()); 
     } 
     setListAdapter(new ArrayAdapter<String>(this, R.layout.row, 
       R.id.bucket_list, bucketList)); 
    } 

MY .XML如下:

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

    <ListView 
     android:id="@id/android:list" 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:drawSelectorOnTop="false"/> 

    <TextView 
     android:id="@id/android:empty" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:text="@string/noData"/> 


</LinearLayout> 

行佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:background="@color/lightgray"> 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:contentDescription="@string/none" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/bucket_list" 
     android:layout_width="210dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="15dp" 
     android:layout_marginTop="15dp"/> 

    <TextView 
     android:id="@+id/bucket_percentage" 
     android:layout_marginTop="15dp" 
     android:layout_width="40dp" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="10dp" 
    /> 

    <TextView 
     android:id="@id/android:hidden_id" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:visibility="gone" 
    /> 

</LinearLayout> 

我怎樣才能使項目列表中的所有三個字段? 謝謝。

+0

你可以看到[這](http://www.ezzylearning.com/tutorial.aspx?tid=1763429)用於創建自定義適配器爲listview –

回答

1

通過擴展ArrayAdapter創建自己的自定義適配器,並覆蓋getView()方法

+0

你有任何我可以學習的教程鏈接?謝謝。 – newbie

+0

http://www.ezzylearning.com/tutorial.aspx?tid=1763429。希望這可以幫助 –

相關問題