2014-08-28 55 views
0

我一直在尋找如何將一個Arraylist放到Android的ListView上,但一直未能這樣做。這是我使用的程序,但它似乎只適用於String數組,而不適用於List元素。在android前端顯示arraylist

List<Students> studentList = new ArrayList<Students>(); 
ArrayAdapter<Students> arrayAdapter = new ArrayAdapter<Students>(
       this, 
       android.R.layout.simple_list_item_single_choice, 
       studentList); 
listView.setAdapter(arrayAdapter); 

我需要找到一個解決方案,我可以把這個數組列表放到列表視圖中,而不必將其轉換爲STring數組。有這樣的事情嗎? 在此先感謝。 :-)

+0

你必須做出一個自定義適配器 – Apoorv 2014-08-28 09:11:07

+0

使用自定義適配器類 – Piyush 2014-08-28 09:12:41

回答

1

你將不得不覆蓋適配器的getView方法來實現所需的功能:

ArrayAdapter<Students> arrayAdapter = new ArrayAdapter<Students>(
     this, 
     android.R.layout.simple_list_item_single_choice, 
     studentList) { 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // Let the system create and re-use the views 
     View view = super.getView(position, convertView, parent); 

     // Get the default android layout's TextView 
     TextView textView = (TextView) view.findViewById(android.R.id.text1); 

     // Get the students info (name or w/e) 
     Students student = getItem(position); 
     String studentName = student.getName(); 
     textView.setText(studentName); 

     return view; 
    } 
};