The data that you setup as a string-array
is not properly built to be shown in a ListView
. It should be like this:
<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>
To parse this in a data structure good for a ListView
you would write(part of the code comes from this answer: 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();
The Exam
class is a simple data holder class:
public class Exam {
String name, desc, date;
public Exam(String[] dataArray) {
this.name = dataArray[0];
this.desc = dataArray[1];
this.date = dataArray[2];
}
}
Then you would use the extractedData
ArrayList
in a custom adapter with your row layout:
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;
}
}