string-array
으로 설정 한 데이터가 올바르게 빌드되어 ListView
에 표시되지 않습니다. 그것은 다음과 같이해야합니다 :
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
클래스하십시오 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>
가 (: Android Resource - Array of Arrays 코드의 일부는이 답변에서 오는) 좋은 데이터 구조에서이 구문 분석
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;
}
}
고마워요! 나는 가야한다. 그러나 내가 돌아 왔을 때 나는 그것을 읽을 것이고, 무엇인가 이해되지 않으면 논평 할 것이다. .. 고마워! –
질문이 있습니다 - CustomAdapter는 무엇입니까? 새로운 수업으로 추가해야합니까? 그리고 그렇다면 - 메인 클래스와 CustomAdapter를 어떻게 연결합니까? –
@RoniCopul 안드로이드의 'ListView'는 데이터를 제공하는 'Adapter'유형의 데이터와 화면에 표시 할 행보기를 필요로하는 데이터 목록을 나타냅니다. 방금 사용자 지정 어댑터를 만들었으므로 기본 어댑터가 데이터를 제공하는 방식을 수정할 수 있습니다.'custom adapter android' 이후의 구글 검색은 필요한 모든 정보를 제공해야합니다 (여기 튜토리얼 http://www.vogella.com/articles/AndroidListView/article.html의 예가 있습니다). – Luksprog