リストを含むアプリを作成していますが、リスト内のすべてのアイテムにいくつかの値(名前、説明、日付)があります。リスト内の任意のアイテムの構造を含むXMLファイルを作成しました。
また、リスト内のアイテムを含む別のXMLファイルを取得しました(すべての<item>
タグには、、<name>
および<desc>
子<date>
があります)
問題は、すべてを適切な場所に配置する方法がわからないことです。Webで検索しました。 XML解析と呼ばれていることがわかりましたが、私が見つけたチュートリアルはこれだけでしたが、書かれていないので理解
できませんでした。誰かが説明したり、良いチュートリアルを教えてくれませんか。
2 に答える
2
として設定したデータは、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リソース-配列の配列):
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
クラスは、単純なデータホルダークラスです。
public class Exam {
String name, desc, date;
public Exam(String[] dataArray) {
this.name = dataArray[0];
this.desc = dataArray[1];
this.date = dataArray[2];
}
}
次に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-29T12:21:31.093 に答える
0
私は最近、このチュートリアルに従って、AndroidでXMLを解析する方法を学びました
http://developer.android.com/training/basics/network-ops/xml.html
それが役に立てば幸い :)
于 2012-07-28T21:28:25.540 に答える