MVC架构
M:模型层---javabean----personList
V:视图层---jsp----ListView
C:控制层----servlet---Adapter
Adapter
M:模型层---javabean----personList
V:视图层---jsp----ListView
C:控制层----servlet---Adapter
Adapter
ListView的每一个条目都是View对象
XML布局文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>
//java文件
public class MainActivity extends Activity {
List<Person> personList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
personList = new ArrayList<Person>();
//把数据库的数据查询出来
MyOpenHelper oh = new MyOpenHelper(this, "person.db", null, 1);
SQLiteDatabase db = oh.getWritableDatabase();
Cursor cursor = db.query("person", null, null, null, null, null, null, null);
while(cursor.moveToNext()){
String _id = cursor.getString(0);
String name = cursor.getString(1);
String phone = cursor.getString(2);
String salary = cursor.getString(3);
Person p = new Person(_id, name, phone, salary);
personList.add(p);
}
ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(new MyAdapter());
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return personList.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;
}
//由系统调用,获取一个View对象,作为ListView的条目
//position本次getView方法调用返回的view对象,在listView中处于第几个条目
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView tv = new TextView(MainActivity.this);
Person p = personList.get(position);
System.out.println("position" + position);
tv.setText(p.toString());
return tv;
}
}