activity_main
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
MainActivity
package org.gentry.db;
import java.util.List;
import org.gentry.db.dao.PersonDao;
import org.gentry.db.domain.Person;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ListView lv;
private List<Person> persons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PersonDao dao = new PersonDao(this);
persons = dao.findAll();
lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(new MyAdapter());
}
private class MyAdapter extends BaseAdapter {
/**
* 控制listview里面总共有多少条目
*/
@Override
public int getCount() {
// TODO Auto-generated method stub
return persons.size(); // 条目个数==集合的size
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView tv = new TextView(getApplicationContext());
tv.setTextSize(20);
tv.setTextColor(Color.BLACK);
Person person = persons.get(position); // 得到某个位置对应的person对象
tv.setText(person.toString());
return tv;
}
}
}