Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> android ListView_顯示數據庫數據,androidlistview

android ListView_顯示數據庫數據,androidlistview

編輯:關於android開發

android ListView_顯示數據庫數據,androidlistview


xml

<?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" android:orientation="vertical" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:id="@+id/bt_add" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/add"/> <Button android:id="@+id/bt_del" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/del"/> <Button android:id="@+id/bt_update" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/update"/> <Button android:id="@+id/bt_query" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/query"/> <ListView android:id="@+id/lv_databases" android:layout_height="match_parent" android:layout_width="fill_parent"/> </LinearLayout> 數據庫按鈕設計

 

java

package com.itheima.crud; import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ListView; import android.widget.Toast; import com.itheima.crud2_listview.R; import com.itheima.crud.adapter.QueryAdapter; import com.itheima.crud.bean.InfoBean; import com.itheima.crud.dao.InfoDao; public class MainActivity extends Activity implements OnClickListener { private Context mContext; private ListView lv_databases; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this; //創建一個幫助類對象 MySqliteOpenHelper mySqliteOpenHelper = new MySqliteOpenHelper(mContext); //調用getReadableDatabase方法,來初始化數據庫的創建 SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase(); //找到相應控件 findViewById(R.id.bt_add).setOnClickListener(this); findViewById(R.id.bt_del).setOnClickListener(this); findViewById(R.id.bt_update).setOnClickListener(this); findViewById(R.id.bt_query).setOnClickListener(this); //找到listview lv_databases = (ListView) findViewById(R.id.lv_databases); } @Override public void onClick(View v) { InfoDao infoDao = new InfoDao(mContext);//創建一個dao對象做增刪改查 switch (v.getId()) { case R.id.bt_add: InfoBean bean = new InfoBean(); bean.name = "張三"; bean.phone ="110"; boolean result = infoDao.add(bean); if(result){ Toast.makeText(mContext, "添加成功", 0).show(); }else{ Toast.makeText(mContext, "添加失敗", 0).show(); } break; case R.id.bt_del: int del = infoDao.del("張三"); Toast.makeText(mContext, "成功刪除"+del+"行", 0).show(); break; case R.id.bt_update: InfoBean bean2 = new InfoBean(); bean2.name = "張三"; bean2.phone ="119"; int update = infoDao.update(bean2); Toast.makeText(mContext, "成功修改"+update+"行", 0).show(); break; case R.id.bt_query: //獲取查詢的數據 ArrayList<InfoBean> arrayList = infoDao.query("張三"); //封裝adapter QueryAdapter queryAdapter = new QueryAdapter(mContext,arrayList); //將adapter設置給listview lv_databases.setAdapter(queryAdapter); break; default: break; } } } MainActivity

 

package com.itheima.crud; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class MySqliteOpenHelper extends SQLiteOpenHelper { public MySqliteOpenHelper(Context context) { //context :上下文 , name:數據庫文件的名稱 factory:用來創建cursor對象,默認為null //version:數據庫的版本號,從1開始,如果發生改變,onUpgrade方法將會調用,4.0之後只能升不能將 super(context, "info.db", null,1); } //oncreate方法是數據庫第一次創建的時候會被調用; 特別適合做表結構的初始化,需要執行sql語句;SQLiteDatabase db可以用來執行sql語句 @Override public void onCreate(SQLiteDatabase db) { //通過SQLiteDatabase執行一個創建表的sql語句 db.execSQL("create table info (_id integer primary key autoincrement,name varchar(20),phone varchar(11))"); } //onUpgrade數據庫版本號發生改變時才會執行; 特別適合做表結構的修改 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //添加一個phone字段 // db.execSQL("alter table info add phone varchar(11)"); } } MySqliteOpenHelper

 

package com.itheima.crud.dao; import java.util.ArrayList; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.itheima.crud.MySqliteOpenHelper; import com.itheima.crud.bean.InfoBean; public class InfoDao { private MySqliteOpenHelper mySqliteOpenHelper; public InfoDao(Context context){ //創建一個幫助類對象 mySqliteOpenHelper = new MySqliteOpenHelper(context); } public boolean add(InfoBean bean){ //執行sql語句需要sqliteDatabase對象 //調用getReadableDatabase方法,來初始化數據庫的創建 SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase(); ContentValues values = new ContentValues();//是用map封裝的對象,用來存放值 values.put("name", bean.name); values.put("phone", bean.phone); //table: 表名 , nullColumnHack:可以為空,標示添加一個空行, values:數據一行的值 , 返回值:代表添加這個新行的Id ,-1代表添加失敗 long result = db.insert("info", null, values);//底層是在拼裝sql語句 //關閉數據庫對象 db.close(); if(result != -1){//-1代表添加失敗 return true; }else{ return false; } } public int del(String name){ //執行sql語句需要sqliteDatabase對象 //調用getReadableDatabase方法,來初始化數據庫的創建 SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase(); //table :表名, whereClause: 刪除條件, whereArgs:條件的占位符的參數 ; 返回值:成功刪除多少行 int result = db.delete("info", "name = ?", new String[]{name}); //關閉數據庫對象 db.close(); return result; } public int update(InfoBean bean){ //執行sql語句需要sqliteDatabase對象 //調用getReadableDatabase方法,來初始化數據庫的創建 SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase(); ContentValues values = new ContentValues();//是用map封裝的對象,用來存放值 values.put("phone", bean.phone); //table:表名, values:更新的值, whereClause:更新的條件, whereArgs:更新條件的占位符的值,返回值:成功修改多少行 int result = db.update("info", values, "name = ?", new String[]{bean.name}); //關閉數據庫對象 db.close(); return result; } public ArrayList<InfoBean> query(String name){ ArrayList<InfoBean> list = new ArrayList<InfoBean>(); //執行sql語句需要sqliteDatabase對象 //調用getReadableDatabase方法,來初始化數據庫的創建 SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase(); //table:表名, columns:查詢的列名,如果null代表查詢所有列; selection:查詢條件, selectionArgs:條件占位符的參數值, //groupBy:按什麼字段分組, having:分組的條件, orderBy:按什麼字段排序 Cursor cursor = db.query("info", new String[]{"_id","name","phone"}, "name = ?", new String[]{name}, null, null, "_id asc"); //解析Cursor中的數據 if(cursor != null && cursor.getCount() >0){//判斷cursor中是否存在數據 //循環遍歷結果集,獲取每一行的內容 while(cursor.moveToNext()){//條件,游標能否定位到下一行 InfoBean bean = new InfoBean(); //獲取數據 bean. id = cursor.getInt(0)+""; bean. name = cursor.getString(1); bean. phone = cursor.getString(2); list.add(bean); } cursor.close();//關閉結果集 } //關閉數據庫對象 db.close(); return list; } } 包 dao

 

package com.itheima.crud.bean; public class InfoBean { public String id; public String name; public String phone; } 包 bean

 

package com.itheima.crud.adapter; import java.util.ArrayList; import com.itheima.crud.bean.InfoBean; import com.itheima.crud2_listview.R; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class QueryAdapter extends BaseAdapter{ private Context mContext; private ArrayList<InfoBean> arrayList; public QueryAdapter(Context mContext, ArrayList<InfoBean> arrayList) { this.mContext = mContext; this.arrayList = arrayList; } @Override public int getCount() { return arrayList.size(); } @Override public Object getItem(int position) { return arrayList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { //復用convertView View view = null; if(convertView != null){ view = convertView; }else { view = View.inflate(mContext, R.layout.item_database_layout, null); } //找到控件 TextView item_tv_id = (TextView) view.findViewById(R.id.item_tv_id); TextView item_tv_name = (TextView) view.findViewById(R.id.item_tv_name); TextView item_tv_phone = (TextView) view.findViewById(R.id.item_tv_phone); //找到內容 InfoBean infoBean = arrayList.get(position); //設置內容 item_tv_id.setText(infoBean.id); item_tv_name.setText(infoBean.name); item_tv_phone.setText(infoBean.phone); return view; } } 包 adapter

 

新聞會了,這個就會了

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved