AFAIK simple_list_item_2包含一個包含兩個TextView的TwoLineListItem。 ArrayAdapter不會在這裏工作,您必須創建一個自定義適配器或使用支持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);
或者,如果你不想SimpleCursorAdapter你必須創建一個自定義ArrayAdapter或BaseAdapter
創建自定義ArrayAdapter,應用(有兩項)陣列的自定義適配器的對象,將其提供給getListView.setAdapter。
重寫ArrayAdapter的getView方法,將您的名稱字符串應用於自定義列表行視圖中的TextView。
以下代碼將幫助您。
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;
}
}
您使用的getter /在'Person.Java'制定者這是後話谷歌對他們的業績指南的建議。 – Fred
@Fred Android的性能提示頁面建議開發人員「避免使用INTERNAL getters/setter」,他們似乎指的是訪問成員變量的方法。這並不意味着你不應該編寫具有適合世界其他地方與之交互的適當界面的類。在同一篇文章中,他們還指出ProGuard將吸氣劑和吸附劑列入其中,這使得整個研究成果毫無意義。 – spaaarky21
這是一個很好的答案,在代碼中提供了所需的信息和非常實用的應用程序。 – LarsH