Using the ListView to display the contents of the database
- Create a trigger mechanism---------(function) to read the data into the ArrayList collection
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 ()) {//write data to 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 (); Load Adapter
Mybase.notifydatasetchanged ();//Flush Adapter
Lv.setadapter (myBase);
- Define a ListView-----(action) put data from the collection into the ListView
- Find the ListView
- Define a complex Baseadapeter
A) Implementation method
Public int GetCount ()//Returns the number of data
Public View GetView (int arg0, view arg1, ViewGroup arg2)//Return view value
View view = view. Inflate (Mainactivity. this, r.layout. item, null); Convert XML to view
TextView tv_name = (TextView) View.findviewbyid (r.id. Item_text_1);//Get Child
TextView tv_num = (TextView) View.findviewbyid (r.id. Item_text_2);
Tv_name.settext (List.get (arg0). GetName ());//Data loading
Tv_num.settext (List.get (arg0). Getnum ());
- Set adapter for the ListView
Using the ListView to display the contents of the database