安卓加载mysql数据到列表里_Android开发之初级开发_动态加载数据库中数据到Listview中...
importjava.util.ArrayList;importjava.util.List;importandroid.content.ContentValues;importandroid.content.Context;importandroid.database.Cursor;importandroid.database.sqlite.SQLiteDatabase;importcom.hq
importjava.util.ArrayList;importjava.util.List;importandroid.content.ContentValues;importandroid.content.Context;importandroid.database.Cursor;importandroid.database.sqlite.SQLiteDatabase;importcom.hql.sqlite.StudentOpenhelper;importcom.hql.sqlite.domain.StudentInfo;public classStudent
{privateStudentOpenhelper helper;publicStudent(Context context)
{
helper= newStudentOpenhelper(context);
}/*** 添加一条记录
*
*@paramname
*@paramnumber*/
public longadd(String name, String number)
{
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues values= newContentValues();
values.put("name", name);
values.put("number", number);long id = db.insert("student", null, values);
db.close();returnid;
}/*** 查看
*
*@paramname
*@return
*/
public booleanfind(String name)
{
SQLiteDatabase db=helper.getReadableDatabase();
Cursor cursor= db.query("student", null, "name=?", newString[]
{ name },null, null, null);boolean result =cursor.moveToNext();
cursor.close();
db.close();returnresult;
}/*** 更新记录
*
*@paramname
*@paramnewnumber
*@return
*/
public intupdate(String name, String newnumber)
{
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues values= newContentValues();int number = db.update("student", values, "name=?", newString[]
{ name });
db.close();returnnumber;
}/*** 删除记录
*
*@paramname
*@return
*/
public intdelete(String name)
{
SQLiteDatabase db=helper.getWritableDatabase();int delete = db.delete("student", "name=?", newString[]
{ name });
db.close();returndelete;
}/*** 查询所有记录
*
*@return
*/
public ListfindAll()
{
List infos = new ArrayList();
SQLiteDatabase db=helper.getWritableDatabase();
Cursor cursor= db.query(true, "student", newString[]
{"id", "name", "number" }, null, null, null, null, null, null);while(cursor.moveToNext())
{int id = cursor.getInt(cursor.getColumnIndex("id"));
String name= cursor.getString(cursor.getColumnIndex("name"));
String number= cursor.getString(cursor.getColumnIndex("number"));
StudentInfo info= newStudentInfo(id, name, number);
infos.add(info);
}
cursor.close();
db.close();returninfos;
}
}
更多推荐




所有评论(0)