Using the ListView to display the contents of the database

Source: Internet
Author: User

Using the ListView to display the contents of the database

    1. 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);

    1. Define a ListView-----(action) put data from the collection into the ListView
    2. Find the ListView
    3. 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 ());

    1. Set adapter for the ListView

Using the ListView to display the contents of the database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.