Bind data in SQLite using a ListView

Source: Internet
Author: User
Tags sqlite sqlite database



I want to implement a feature in my Android project that reads data from a SQLite database and displays it in a ListView control on a page.



First, I set up a service class to implement various operations on the database, and then add additions and deletions to the database operations in this class. The specific code is as follows:


Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;

Import android.content.ContentValues;
Import Android.content.Context;
Import Android.database.Cursor;
Import Android.database.sqlite.SQLiteDatabase;

Import Com.example.cardemo1.Username;
Import Com.example.cardemo1.db.DBHelper;

public class Carservice {
Private DBHelper DBHelper;


Public Carservice (Context context) {
Dbhelper=new DBHelper (context);

}

Add vehicle information
public boolean register (Car car,string num) {

Sqlitedatabase SDB = Dbhelper.getreadabledatabase ();
String str = "Select Carnumber from car where carnumber =?";
cursor cursor = sdb.rawquery (str, new string[]{num});
if (Cursor.movetofirst () = True) {
String sql = "INSERT into car (_id,carbrand,carsign,carmodel,carnumber,enginenum,bodylevel,mileage,gasoline, Engineperfor,transperfor,carlight) VALUES (?,?,?,?,?,?,?,?,?,?,?,?) ";
Object obj[]={car.get_id (), Car.getcarbrand (), Car.getcarsign (), Car.getcarmodel (), Car.getcarnumber (), Car.getenginenum (),
Car.getbodylevel (), Car.getmileage (), Car.getgasoline (), Car.getengineperfor (), Car.gettransperfor (), Car.getcarlight ()};
Sdb.execsql (Sql,obj);
return true;
}else{
return false;
}
}

//Check vehicle information, return type is ArrayList

Public ArrayList Select (String _id) {


arraylist


sqlitedatabase SDB = Dbhelper.getreadabledatabase ();

String sql= "select * from car where _id=?";

cursor cursor = sdb.rawquery (sql, new string[]{_id});


While (Cursor.movetonext ()) {

hashmap<string, string> map= new hashmap<string, string> ();

map.put ("Carbrand", cursor.getstring (1));

map.put ("Carsign", cursor.getstring (2));

map.put ("Carmodel", Cursor.getstring (3));

map.put ("Carnumber", cursor.getstring (4));

map.put ("Enginenum", cursor.getstring (5));

map.put ("Bodylevel", cursor.getstring (6));

map.put ("Mileage", cursor.getstring (7));

map.put ("Gasoline", cursor.getstring (8));

map.put ("Engineperfor", cursor.getstring (9));

map.put ("Transperfor", Cursor.getstring (Ten));

map.put ("Carlight", Cursor.getstring (one));

list.add (map);

       }

return list;


   }


Query the license plate number, the return type is an array

Public string[] Select_carnum (String _id) {

string[] Carnum = new STRING[20];

String Carnumber;

int index;

Sqlitedatabase SDB = Dbhelper.getreadabledatabase ();

String sql= "Select Carnumber from car where _id=?";

cursor cursor = sdb.rawquery (sql, new string[]{_id});

int i = 0;

while (Cursor.movetonext ()) {

index = Cursor.getcolumnindex ("Carnumber");

Carnum[i] = cursor.getstring (index);

i++;

}

return carnum;


}

Update vehicle Information

public boolean update (Car car,string num) {

Sqlitedatabase SDB = Dbhelper.getreadabledatabase ();

String str = "Select Carnumber from car where carnumber =?";

cursor cursor = sdb.rawquery (str, new string[]{num});

if (cursor.movetofirst () = = True) {

String sql= "Update car set carbrand=?,carsign=?,carmodel=?,enginenum=?,bodylevel=?,mileage=?,gasoline=?, Engineperfor=?,transperfor=?,carlight=? where _id =? ";

Object Obj[]={car.getcarbrand (), Car.getcarsign (), Car.getcarmodel (), Car.getenginenum (),

Car.getbodylevel (), Car.getmileage (), Car.getgasoline (), Car.getengineperfor (), Car.gettransperfor (), Car.getcarlight (), car.get_id ()};

Sdb.execsql (Sql,obj);

return true;

}else

{

String sql= "Update car set carbrand=?,carsign=?,carmodel=?,carnumber=?,enginenum=?,bodylevel=?,mileage=?,gasoline= ?, engineperfor=?,transperfor=?,carlight=? where _id =? ";

Object Obj[]={car.getcarbrand (), Car.getcarsign (), Car.getcarmodel (), Car.getcarnumber (), Car.getenginenum (),

Car.getbodylevel (), Car.getmileage (), Car.getgasoline (), Car.getengineperfor (), Car.gettransperfor (), Car.getcarlight (), car.get_id ()};

Sdb.execsql (Sql,obj);

return true;

}


}

The bold one is the data I want to extract and bind to the ListView. And that's what I wrote in the activity class:




Username uname = (Username) getapplicationcontext ();

String name = Uname.getusername ();

Carservice service = new Carservice (carseeactivity.this);

Call the Select method in the service to get the data in the database






Service.select (name);






Instantiating a ListView space

ListView LV = (ListView) Findviewbyid (r.id.lv);






Create Adapter

Simpleadapter adapter = new Simpleadapter (carseeactivity.this, Service.select (name), R.layout.item, New string[]{" Carbrand "," Carsign "," Carmodel "," Carnumber "," Enginenum "," Bodylevel "," mileage "," Gasoline "," engineperfor "," Transperfor "," Carlight "},

New Int[]{r.id.car_carbrand,r.id.car_carsign,r.id.car_carmodel,r.id.car_carnumber,r.id.car_enginenum,r.id.car_ Bodylevel,r.id.car_mileage,r.id.car_gasoline,r.id.car_engineperfor,r.id.car_transperfor,r.id.car_light});

Bind the ListView and Adapter

Lv.setadapter (adapter);






Set Up Click events

Lv.setonitemclicklistener (New Itemclicklistener ());












The first two sentences are to get the user name, in order to find in the database, the user name can be obtained.




public class Itemclicklistener implements onitemclicklistener{


@Override

public void Onitemclick (adapterview<?> parent, view view, int position,

Long id) {

TODO auto-generated Method Stub

ListView ListView = (listview) Parent;

hashmap<string, string> data = (hashmap<string, string>) listview.getitematposition (position);

String Carid = Data.get ("Carnumber");

Toast.maketext (Getapplicationcontext (), "license plate number for" +carid+ "Vehicle Information", 1). Show ();


}










Bind data in SQLite using a ListView




Related Article

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.