2013-04-08 44 views
2

我正在開發一個用於創建學生Marklist的應用程序ListView在Android中使用TextView,TextView,RadioGroup自定義ListView

在這個應用程序中,列表中有10名學生。爲這些學生提供的考試有四個等級。一個學生只能適應四個中的一個。

老師會將分數分配給ListView中的學生。

我的XML文件Studentlist.xml情況如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 
<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</ListView> 
</LinearLayout> 

和我row.xml文件如下:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="500dp" 
android:layout_height="fill_parent" 
android:padding="10dp" > 

<TableRow> 

    <TextView 
     android:id="@+id/SNo" 
     style="@style/text" 
     android:layout_width="40dp" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/StudNo" 
     style="@style/text" 
     android:layout_width="80dp" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/StudName" 
     style="@style/text" 
     android:layout_width="180dp" 
     android:layout_height="wrap_content" /> 

    <RadioGroup 
     android:id="@+id/radioGroup1" 
     android:layout_width="160dp" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <RadioButton 
      android:id="@+id/radio0" 
      android:layout_width="40dp" 
      android:layout_height="wrap_content" 
      android:checked="true" /> 

     <RadioButton 
      android:id="@+id/radio1" 
      android:layout_width="40dp" 
      android:layout_height="wrap_content" /> 

     <RadioButton 
      android:id="@+id/radio2" 
      android:layout_width="40dp" 
      android:layout_height="wrap_content" /> 

     <RadioButton 
      android:id="@+id/radio3" 
      android:layout_width="40dp" 
      android:layout_height="wrap_content" /> 
    </RadioGroup> 
</TableRow> 

現在我嘗試輸出的形式如下:

1 0001 AAAA <RadioButton1> <RadioButton2> <RadioButton3> <RadioButton4> 
2 0002 BBBB <RadioButton1> <RadioButton2> <RadioButton3> <RadioButton4> 

如何使用適配器功能?

ArrayAdapter<String,String,String,RadiGroup> studList=new ArrayAdapter<String,String,String,RadiGroup>();

我可以使用這樣的,以及如何開發爲ListView定製適配器?

建議我尋求最佳解決方案!

+0

?常規的LinearLayout或RelativeLayout也適用。 – etienne 2013-04-08 15:33:04

+0

你需要定義一個自定義的適配器,這個目的.http://stackoverflow.com/questions/15832335/android-custom-row-item-for-listview/15832695#15832695 – Raghunandan 2013-04-08 15:35:54

+0

兩者都是一樣的,對..我們正在取代通過自定義佈局在列表視圖中列出項目。在這裏LinearLayout和TableLayout是一樣的,我們可以使用任何東西。告訴我一個關於LinearLayout解決方案的建議。 – user2258206 2013-04-08 15:36:27

回答

1

您可以使用從ArrayAdapter繼承的類,並重寫其getView()方法。

public class StudentAdapter extends ArrayAdapter<Student> { 

protected LayoutInflater inflater; 

    public StudentAdapter(final Context context) { 
     super(context, 0); 
     inflater = (LayoutInflater) ((Context) context) 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

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

     // Note: You should optimize here with re-using convertView 

     View rowView = inflater.inflate(R.layout.user_address_row_layout, 
       parent, false); 
     TextView sNo = (TextView) rowView.findViewById(R.id.sNo); 
     sNo.setText(getItem(position).number); 
      // same for every field of the row 
      // ... 

     return rowView; 
    } 

} 
+0

您沒有使用View Holder。 http://www.youtube.com/watch?v=wDBM6wVEO70 – Raghunandan 2013-04-08 15:43:19

+0

同意,但你應該更有效地檢查convertView是否已經存在,只需要更新視圖的情況。在這裏你有一個使用ViewHolder模式的例子:https://github.com/Jachu5/RssFeeder_sherlock/blob/master/src/com/example/adapter/RSS_CustomAdapter.java – Jachu 2013-04-08 15:45:35

+0

這就是爲什麼我指定'你應該優化這裏[等] '在評論中。我沒有用它來保持例子簡單。 – etienne 2013-04-08 17:17:19

1

這是一個用於listview的自定義適配器的示例。修改它作爲你的需要: XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:id="@+id/main" 
    android:orientation="vertical" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- Header --> 

    <LinearLayout 
     android:id="@+id/header" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/black" > 

     <TextView 
      android:id="@+id/item2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:height="30dip" 
      android:text="Latest News - Your Healing Place" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="#FFFFFF" 
      android:width="100dip" /> 

     <TextView 
      android:id="@+id/item1" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:height="30dip" 
      android:width="20dip" /> 

    </LinearLayout> 

    <View android:layout_width="fill_parent" 
     android:layout_height="1dip" 
     android:background="?android:attr/listDivider" /> 

    <LinearLayout android:id="@+id/layout" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent"> 

     <ListView 
      android:id="@+id/listview" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@color/black" > 

     </ListView> 

    </LinearLayout> 
</LinearLayout> 

news.java:你爲什麼要使用TableLayout該行

List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>(); 
String[] from = new String[] {"details", "date"}; 
int[] to = new int[] { R.id.item1, R.id.item2}; 
ListView lv= (ListView)findViewById(R.id.listview); 
HashMap<String, String> map = new HashMap<String, String>(); 
map.put("details", "This is a sample message"); 
map.put("date", "24-07-2003"); 
fillMaps.add(map); 

SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to); 
lv.setAdapter(adapter); 
相關問題