AFAIK simple_list_item_2 chứa một TwoLineListItem chứa hai TextView. ArrayAdapter sẽ không hoạt động ở đây, bạn sẽ phải tạo một adapter tùy chỉnh hoặc sử dụng một adapter hỗ trợ nó như SimpleCursorAdapter.
ListAdapter adapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_2,
mCursor, // Pass in the cursor to bind to.
new String[] {People.NAME, People.COMPANY}, // Array of cursor columns to bind to.
new int[] {android.R.id.text1, android.R.id.text2}); // Parallel array of which template objects to bind to those columns.
// Bind to our new adapter.
setListAdapter(adapter);
Hoặc nếu bạn không muốn SimpleCursorAdapter Bạn sẽ phải tạo Custom ArrayAdapter hoặc BaseAdapter
Tạo một ArrayAdapter tùy chỉnh, áp dụng các đối tượng (Có hai bài) mảng để adapter tùy chỉnh, và nạp nó vào getListView.setAdapter.
Ghi đè phương thức getView của ArrayAdapter để áp dụng các chuỗi tên của bạn cho TextViews trong chế độ xem hàng danh sách tùy chỉnh của bạn.
Đoạn mã sau sẽ giúp bạn.
SampleActivity.java
package org.sample;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.TwoLineListItem;
public class SampleActivity extends ListActivity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Person person;
ArrayList<Person> persons = new ArrayList<Person>();
person = new Person();
person.setName("Vipul");
person.setAge(20);
persons.add(person);
person = new Person();
person.setName("Anil");
person.setAge(22);
persons.add(person);
setListAdapter(new MyAdapter(this, persons));
}
}
Person.java
class Person {
String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
MyAdapter.java
class MyAdapter extends BaseAdapter {
private Context context;
private ArrayList<Person> persons;
public MyAdapter(Context context, ArrayList<Person> persons) {
this.context = context;
this.persons = persons;
}
@Override
public int getCount() {
return persons.size();
}
@Override
public Object getItem(int position) {
return persons.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TwoLineListItem twoLineListItem;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
twoLineListItem = (TwoLineListItem) inflater.inflate(
android.R.layout.simple_list_item_2, null);
} else {
twoLineListItem = (TwoLineListItem) convertView;
}
TextView text1 = twoLineListItem.getText1();
TextView text2 = twoLineListItem.getText2();
text1.setText(persons.get(position).getName());
text2.setText("" + persons.get(position).getAge());
return twoLineListItem;
}
}
Nguồn
2012-06-29 05:49:36
Bạn sử dụng getters/setters trong 'Person.Java' đây là một cái gì đó Lời khuyên của Google chống lại trong hướng dẫn hiệu suất của họ. – Fred
@Fred Android's Performance Tips trang khuyên nhà phát triển "tránh Getters/setters nội bộ", theo đó họ dường như đang đề cập đến các phương pháp truy cập vào biến thành viên.Điều đó không có nghĩa là bạn không nên viết các lớp của bạn với các giao diện thích hợp cho phần còn lại của thế giới để tương tác với. Và trong cùng một mẹo, họ cũng chỉ ra rằng ProGuard inline getters và setters, làm cho toàn bộ điểm tranh luận. – spaaarky21
Đây là một câu trả lời xuất sắc, cung cấp cả thông tin được yêu cầu và các ứng dụng rất thực tế trong mã. – LarsH