您設置爲string-array
的數據未正確構建以顯示在ListView
中。它應該是這樣的:
<string-array name="exams">
<item>@array/exam1</item>
<item>@array/exam2</item>
<item>@array/exam3</item>
<item>@array/exam4</item>
</string-array>
<string-array name="exam1">
<item>One</item>
<item>11111111One</item>
<item>25/7/12</item>
</string-array>
<string-array name="exam2">
<item>Two</item>
<item>2222222222Two</item>
<item>28/7/12</item>
</string-array>
<string-array name="exam3">
<item>Three</item>
<item>333333333333Three</item>
<item>29/1/10</item>
</string-array>
<string-array name="exam4">
<item>Four</item>
<item>444444444Four</item>
<item>21/2/11</item>
</string-array>
在數據結構很好解析此爲ListView
你會寫(代碼的一部分來自這樣的回答:Android Resource - Array of Arrays):
Resources res = getResources();
ArrayList<Exam> extractedData = new ArrayList<Exam>();
TypedArray ta = res.obtainTypedArray(R.array.exams);
int n = ta.length();
for (int i = 0; i < n; ++i) {
int id = ta.getResourceId(i, 0);
if (id > 0) {
extractedData.add(new Exam(res.getStringArray(id)));
} else {
// something wrong with the XML, don't add anything
}
}
ta.recycle();
的Exam
類是一個簡單的數據holder類:
public class Exam {
String name, desc, date;
public Exam(String[] dataArray) {
this.name = dataArray[0];
this.desc = dataArray[1];
this.date = dataArray[2];
}
}
然後你會在自定義ADA使用extractedData
ArrayList
與您的行佈局:
public class CustomAdapter extends ArrayAdapter<Exam> {
private LayoutInflater mInflater;
public CustomAdapter(Context context, int textViewResourceId,
List<Exam> objects) {
super(context, textViewResourceId, objects);
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(
R.layout.your_layout_file, parent, false);
}
Exam e = getItem(position);
((TextView) convertView.findViewById(R.id.name)).setText(e.name);
((TextView) convertView.findViewById(R.id.desc)).setText(e.desc);
((TextView) convertView.findViewById(R.id.date)).setText(e.date);
return convertView;
}
}
謝謝!我必須去,但是當我回來時,我會閱讀它並評論是否有什麼不明白的東西。非常感謝! – 2012-07-29 12:41:46
我有一個問題 - CustomAdapter是什麼?我應該把它作爲一個新班級加入嗎?如果是的話 - 如何在主類和CustomAdapter之間進行連接? – 2012-07-30 11:36:31
@RoniCopul在Android中代表數據列表的'ListView'需要一個'Adapter'類型的對象,它將提供數據和行視圖放在屏幕上。我剛製作了一個自定義適配器,以便您可以修改默認適配器提供數據的方式。'custom adapter android'後的谷歌搜索應該提供給你所需要的所有信息(這裏是一個教程http://www.vogella.com/articles/AndroidListView/article.html的例子)。 – Luksprog 2012-07-30 12:50:26