用ListView实现对数据库的内容显示
- 创建一个触发机制 ---------(作用)将数据读入ArrayList集合中
MyBase base = new MyBase();
SQLiteDatabase db = mySQLhelpes.getReadableDatabase();
Cursor cursor = db.query("inof", null, null, null, null, null, null);
list = new ArrayList<Fuwu>();
while(cursor.moveToNext()){ //将数据写入ArrayList中
Fuwu fuwu = new Fuwu();
String name = cursor.getString(cursor.getColumnIndex("name") );
String num = cursor.getString(cursor.getColumnIndex("number"));
fuwu.setName(name);
fuwu.setNum(num);
list.add(fuwu);
fuwu = null;
}
db.close();
MyBase myBase = new MyBase(); //加载适配器
myBase.notifyDataSetChanged();//刷新适配器
lv.setAdapter(myBase);
- 定义一个ListView-----(作用)把数据从集合中放入ListView中
- 找到ListView
- 定义一个复杂的BaseAdapeter
a) 实现方法
public int getCount() //返回数据的个数
public View getView(int arg0, View arg1, ViewGroup arg2) //返回view值
View view = View.inflate(MainActivity.this, R.layout.item, null); //将xml转化为view
TextView tv_name = (TextView) view.findViewById(R.id.item_text_1);//得到子孩子
TextView tv_num = (TextView)view.findViewById(R.id.item_text_2);
tv_name.setText(list.get(arg0).getName());//数据载入
tv_num.setText(list.get(arg0).getNum());
- 给ListView设置Adapter