AFAIK simple_list_item_2 contains a TwoLineListItem containing two TextViews. ArrayAdapter is not going to work here,you'll either have to create a custom adapter or use one that supports it like 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);
Or if you dont want SimpleCursorAdapter You will have to create Custom ArrayAdapter or BaseAdapter
Create a custom ArrayAdapter,apply the object(Having two items) array to the custom adapter, and feed it to getListView.setAdapter.
Override the ArrayAdapter's getView method to apply your name strings to TextViews in your custom list row view.
Following Snippet will help you.
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;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…