新聞中心
隨著移動互聯(lián)網的發(fā)展,移動端應用開發(fā)已經成為了一個巨大的市場,而安卓系統(tǒng)作為全球更大的移動操作系統(tǒng)之一,也是移動應用市場中不可或缺的一部分。在安卓應用開發(fā)中,GridView作為一種常用的視圖控件,能夠將數據以網格狀的形式展現(xiàn)給用戶,從而提升用戶體驗。而數據庫則是安卓應用中存儲數據的必備工具,對于使用GridView的開發(fā)者而言,如何在應用中使用數據庫則是必須掌握的技能之一。本文將為大家介紹安卓GridView數據庫的使用指南,幫助開發(fā)者更好地利用GridView展現(xiàn)數據。

一、GridView簡介
GridView是安卓應用開發(fā)中常用的視圖控件之一,它可以將數據以網格狀的形式展現(xiàn)給用戶,非常適合展示圖片、文字等信息。GridView可以自動滾動,可以自定義每個Item的布局,并且可以使用Adapter來設置Item中的數據和相關屬性等。在開發(fā)中,我們可以通過繼承BaseAdapter類,實現(xiàn)自己的Adapter來對GridView進行自定義控制。
二、使用SQLite數據庫
SQLite是一種輕量級的關系型數據庫,它是安卓系統(tǒng)內置的數據庫,非常適合安卓應用的本地數據存儲。在應用中使用SQLite數據庫,需要先創(chuàng)建一個數據庫及相關表,并對表中的數據進行增刪改查等操作。下面我們來看一下具體的實現(xiàn)過程。
1. 創(chuàng)建SQLiteOpenHelper
在Android中使用SQLite數據庫,需要繼承SQLiteOpenHelper類并重寫它的onCreate()、onUpgrade()方法。其中onCreate()方法會在數據庫不存在時被調用,我們可以在其中創(chuàng)建我們需要的數據庫及表;onUpgrade()方法則在數據庫版本變化時被調用,我們可以在其中升級我們的數據庫。
示例:
public class DatabaseHelper extends SQLiteOpenHelper {
private final static String DB_NAME = “my_database”;
private final static int DB_VERSION = 1;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(“CREATE TABLE person (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)”);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(“DROP TABLE IF EXISTS person”);
onCreate(db);
}
}
2. 數據庫的增刪改查
在安卓應用中使用SQLite數據庫,需要對表中的數據進行增刪改查等操作。下面我們來看一下具體的實現(xiàn)過程。
插入數據:
public void insert() {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(“name”, “張三”);
values.put(“age”, 20);
db.insert(“person”, null, values);
db.close();
}
刪除數據:
public void delete() {
SQLiteDatabase db = helper.getWritableDatabase();
String whereClause = “id=?”;
String[] whereArgs = new String[]{“1”};
db.delete(“person”, whereClause, whereArgs);
db.close();
}
修改數據:
public void update() {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(“age”, 21);
String whereClause = “id=?”;
String[] whereArgs = new String[]{“1”};
db.update(“person”, values, whereClause, whereArgs);
db.close();
}
查詢數據:
public void query() {
SQLiteDatabase db = helper.getReadableDatabase();
String[] cols = new String[]{“id”, “name”, “age”};
Cursor cursor = db.query(“person”, cols, null, null, null, null, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(“id”));
String name = cursor.getString(cursor.getColumnIndex(“name”));
int age = cursor.getInt(cursor.getColumnIndex(“age”));
}
cursor.close();
db.close();
}
三、使用GridView展示數據庫數據
在應用中使用GridView展示數據庫數據,需要先通過SQLiteOpenHelper創(chuàng)建數據庫,并進行增刪改查等數據操作;然后通過BaseAdapter繼承類,實現(xiàn)自定義的Adapter并在其中設置GridView中每個Item的數據和相關屬性。
示例:
1. 定義數據項的布局文件:
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:orientation=”horizontal”>
android:id=”@+id/text_name”
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:textSize=”18sp”/>
android:id=”@+id/text_age”
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:textSize=”18sp”/>
2. 實現(xiàn)自定義的Adapter:
public class PersonAdapter extends BaseAdapter {
private Context mContext;
private List mPersonList;
public PersonAdapter(Context context, List list) {
mContext = context;
mPersonList = list;
}
@Override
public int getCount() {
return mPersonList.size();
}
@Override
public Object getItem(int position) {
return mPersonList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_person, null);
holder = new ViewHolder();
holder.name = convertView.findViewById(R.id.text_name);
holder.age = convertView.findViewById(R.id.text_age);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(mPersonList.get(position).getName());
holder.age.setText(mPersonList.get(position).getAge() + “”);
return convertView;
}
static class ViewHolder {
TextView name;
TextView age;
}
}
3. 設置GridView中的數據和屬性:
public class MnActivity extends AppCompatActivity {
private GridView mGridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mn);
mGridView = findViewById(R.id.grid_view);
PersonAdapter adapter = new PersonAdapter(this, queryData());
mGridView.setAdapter(adapter);
}
private List queryData() {
List dataList = new ArrayList();
DatabaseHelper helper = new DatabaseHelper(this);
SQLiteDatabase db = helper.getReadableDatabase();
String[] cols = new String[]{“id”, “name”, “age”};
Cursor cursor = db.query(“person”, cols, null, null, null, null, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(“id”));
String name = cursor.getString(cursor.getColumnIndex(“name”));
int age = cursor.getInt(cursor.getColumnIndex(“age”));
Person person = new Person(id, name, age);
dataList.add(person);
}
cursor.close();
db.close();
return dataList;
}
}
四、
相關問題拓展閱讀:
- 關于android中GridView控件
關于android中GridView控件
因為調了兩次getData(),最后你的gridView的數據源是循環(huán)加了兩遍的全局變量dataList。
方法一:刪除之一個調尺友用getData那行,不用全局變量dataList,在getData方法里面new一個局部的List,返回陵前槐這個局部變量
方法二:不刪之一個調用getData那行,new SimpleAdapter的時候不再調用getData方法,直接悔頌用dataList
android gridview數據庫的介紹就聊到這里吧,感謝你花時間閱讀本站內容,更多關于android gridview數據庫,安卓GridView數據庫使用指南,關于android中GridView控件的信息別忘了在本站進行查找喔。
成都創(chuàng)新互聯(lián)科技有限公司,是一家專注于互聯(lián)網、IDC服務、應用軟件開發(fā)、網站建設推廣的公司,為客戶提供互聯(lián)網基礎服務!
創(chuàng)新互聯(lián)(www.cdcxhl.com)提供簡單好用,價格厚道的香港/美國云服務器和獨立服務器。創(chuàng)新互聯(lián)成都老牌IDC服務商,專注四川成都IDC機房服務器托管/機柜租用。為您精選優(yōu)質idc數據中心機房租用、服務器托管、機柜租賃、大帶寬租用,可選線路電信、移動、聯(lián)通等。
網站題目:安卓GridView數據庫使用指南(androidgridview數據庫)
URL地址:http://www.dlmjj.cn/article/djsgspe.html


咨詢
建站咨詢
