2012-06-19 28 views
7

我在我的ListView中的每個列表項有多個TextView s。我學會了寫一個合適的getView方法,但我不確定我是否使用setAdapter來調用該方法。如何在每個listview有多個textview的情況下設置適配器?

private static String[] project = {"proj1","proj2"}; 
private static String[] workRequests = {"requirement gathering", "design"}; 
private static String[] startDate = {"02/21/2012","07/15/2011"}; 
private static String[] status = {"WIP","DONE"}; 

ListView mListView; 

public class MyDashboardActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mydashboard); 

     final LayoutInflater mInflater = LayoutInflater.from(this); 
     mListView = (ListView)findViewById(R.id.dashboardList); 
     mListView.setAdapter(
       // How do I set the adapter? 
       ); 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     System.out.println("enters"); 
     if(convertView == null){ 
      convertView = LayoutInflater.from(this).inflate(R.layout.mydashboard,null); 
     } 

     ((TextView) convertView.findViewById(R.id.project)).setText(project[position]); 
     ((TextView) convertView.findViewById(R.id.work_request)).setText(workRequests[position]); 
     ((TextView) convertView.findViewById(R.id.start_date)).setText(startDate[position]); 
     ((TextView) convertView.findViewById(R.id.status)).setText(status[position]); 

     return convertView; 
    } 

這是XML佈局:

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

    <!-- Include Action Bar --> 
    <include layout="@layout/actionbar_layout" /> 

    <ListView 
     android:id="@+id/dashboardList" 
     style="@style/LeftHeaderText" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:background="@drawable/innerdashboard_bg" 
     android:textColor="@color/textColor" > 

     <TextView android:id="@+id/project" /> 

     <TextView android:id="@+id/work_request" /> 

     <TextView android:id="@+id/start_date" /> 

     <TextView android:id="@+id/status" /> 

    </ListView> 

</LinearLayout> 

我已經嘗試了一些辦法,其中沒有工作。任何人都可以請建議如何在這種情況下設置適配器?謝謝!

+1

看到這個完整的例子http://android-example-code.blogspot.in/p/dynamic-custoized-list-view-in-android.html – MAC

+0

一些代碼缺失...在哪裏擴展baseAdapter類? ?? –

+0

是你的'Adapter'類的'getView()'部分嗎? – slybloty

回答

15

您需要實現您自己的適配器。我的方法是也定義一個「代表」一個視圖的對象。

下面是一個非常簡單的例子,其中兩個TextViews可以滿足您的需求。

代表視圖(在ListView行)對象:

public class CustomObject { 

    private String prop1; 
    private String prop2; 

    public CustomObject(String prop1, String prop2) { 
     this.prop1 = prop1; 
     this.prop2 = prop2; 
    } 

    public String getProp1() { 
     return prop1; 
    } 

    public String getProp2() { 
     return prop2; 
    } 
} 

下一頁自定義適配器:

public class CustomAdapter extends BaseAdapter { 

    private LayoutInflater inflater; 
    private ArrayList<CustomObject> objects; 

    private class ViewHolder { 
     TextView textView1; 
     TextView textView2; 
    } 

    public CustomAdapter(Context context, ArrayList<CustomObject> objects) { 
     inflater = LayoutInflater.from(context); 
     this.objects = objects; 
    } 

    public int getCount() { 
     return objects.size(); 
    } 

    public CustomObject getItem(int position) { 
     return objects.get(position); 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 
     if(convertView == null) { 
     holder = new ViewHolder(); 
     convertView = inflater.inflate(R.layout.your_view_layout, null); 
     holder.textView1 = (TextView) convertView.findViewById(R.id.id_textView1); 
     holder.textView2 = (TextView) convertView.findViewById(R.id.list_id_textView2); 
     convertView.setTag(holder); 
     } else { 
     holder = (ViewHolder) convertView.getTag(); 
     } 
     holder.textView1.setText(objects.get(position).getprop1()); 
     holder.textView2.setText(objects.get(position).getprop2()); 
     return convertView; 
    } 
} 

現在,您可以定義和設置您的適配器在您的活動:

ArrayList<CustomObject> objects = new ArrayList<CustomObject>(); 
CustomAdapter customAdapter = new CustomAdapter(this, objects); 
listView.setAdapter(customAdapter); 

現在你只需要在對象列表中管理你的CustomObject。 當您想要對ListView進行重新修改時,請不要忘記調用customAdapter.notifyDataSetChanged()

+0

非常感謝,我正在嘗試。 – Harsh

+0

不用客氣,我修正了我的變量名稱中的一些錯誤。 – FabiF

+0

那麼我必須爲每個創建的'ListView'創建自定義適配器嗎? (其中有多個'TextView' ofcourse) – Harsh

4

您的getView()代碼需要進入擴展BaseAdapter或其子類之一的類。

一種方法是在MyDashboardActivity中創建一個私有類。下面是一個簡單的例子(需要一些額外的代碼)。您可能還需要一個自定義對象將所有要顯示在一個列表項中的內容關聯起來。使用一個自定義類型的數組來代替多個數組,該數組具有您正在跟蹤的每個值的屬性。

還有一件事:你的四個TextView應該進入他們自己的佈局文件(參見list_item.xml here)。該項目佈局文件通過自定義適配器的構造函數進行了關聯(我在下面的代碼中添加了一條註釋以突出顯示此內容)。

protected CustomAdapter mAdapter; 

public class MyDashboardActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mydashboard); 

     final LayoutInflater mInflater = LayoutInflater.from(this); 
     mListView = (ListView)findViewById(R.id.dashboardList); 

     mAdapter = new CustomAdapter(this, <array to be adapted>); 
     mListView.setAdapter(mAdapter); 
    } 

    private class CustomAdapter extends ArrayAdapter<String> { 

     protected Context mContext; 
     protected ArrayList<String> mItems; 

     public CustomAdapter(Context context, ArrayList<String> items) { 
      super(context, R.layout.custom_list_item, items); // Use a custom layout file 
      mContext = context; 
      mItems = items; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      System.out.println("enters"); 
      if(convertView == null){ 
       convertView = LayoutInflater.from(this).inflate(R.layout.mydashboard,null); 
      } 

      // You'll need to use the mItems array to populate these... 
      ((TextView) convertView.findViewById(R.id.project)).setText(project[position]); 
      ((TextView) convertView.findViewById(R.id.work_request)).setText(workRequests[position]); 
      ((TextView) convertView.findViewById(R.id.start_date)).setText(startDate[position]); 
      ((TextView) convertView.findViewById(R.id.status)).setText(status[position]); 

      return convertView; 
     } 
    } 
} 
+0

我意識到了!謝謝:) – Harsh

+2

你是一個救星花花公子。我愛你 – A23149577

+1

謝謝你,我正在使用它。但一個小建議:IntelliJ IDEA給出了「充氣(R.layout.mydashboard,null)」的警告,並且這個答案http://stackoverflow.com/a/24832569/253938建議將它改爲「膨脹(R.layout .mydashboard,父母,FALSE)」。我這樣做,IntelliJ停止嘮叨我,它仍然有效。 – RenniePet

2

有幾種方法可以做到這一點。我會告訴你我的方式,它包含兩個佈局,第一個是ListView的自我,另一個是文本應該如何出現在每個列表項中。

listset.xml

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

    <ListView 
     android:id="@+id/shipMenu" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

listitems.xml(你也可以把圖片在這裏,這個想法在這裏控制)

<?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="fill_parent" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/makerID" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
     android:textSize="25dp"/> 

</LinearLayout> 

通過以上我沒有作品還但我打算爲圖標添加ImageView(並且您也可以添加更多的TextView)。這是我的自定義適配器類。

ListAdapter.java

class ListAdapter extends ArrayAdapter <String> 
{ 

    public ListAdapter(Context context, String[] values) { 

     super(context, R.layout.listitems, values); //set the layout that contains your views (not the one with the ListView) 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflater = LayoutInflater.from(getContext()); 
     View view = inflater.inflate(R.layout.listitems, parent, false); //same here. 

     String text = getItem(position); 

     TextView makerID = (TextView) view.findViewById(R.id.makerID); 
     makerID.setText(text); 

     return view; 
    } 

} 

在您的主要活動文件中設置

setContentView (R.layout.listset); 

而且在相同的括號內添加此下方爲的setContentView()

ListAdapter adapter = new ListAdapter(this, MyString[]); //place your String array in place of MyString 

     ListView lv = (ListView) findViewById(R.id.ListViewID); //the ID you set your ListView to. 
     lv.setAdapter(adapter); 

編輯

我對派對看起來有點晚,但也許這會幫助別人。

相關問題