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

一、GridView簡介
GridView是安卓應(yīng)用開發(fā)中常用的視圖控件之一,它可以將數(shù)據(jù)以網(wǎng)格狀的形式展現(xiàn)給用戶,非常適合展示圖片、文字等信息。GridView可以自動滾動,可以自定義每個Item的布局,并且可以使用Adapter來設(shè)置Item中的數(shù)據(jù)和相關(guān)屬性等。在開發(fā)中,我們可以通過繼承BaseAdapter類,實現(xiàn)自己的Adapter來對GridView進行自定義控制。
二、使用SQLite數(shù)據(jù)庫
SQLite是一種輕量級的關(guān)系型數(shù)據(jù)庫,它是安卓系統(tǒng)內(nèi)置的數(shù)據(jù)庫,非常適合安卓應(yīng)用的本地數(shù)據(jù)存儲。在應(yīng)用中使用SQLite數(shù)據(jù)庫,需要先創(chuàng)建一個數(shù)據(jù)庫及相關(guān)表,并對表中的數(shù)據(jù)進行增刪改查等操作。下面我們來看一下具體的實現(xiàn)過程。
1. 創(chuàng)建SQLiteOpenHelper
在Android中使用SQLite數(shù)據(jù)庫,需要繼承SQLiteOpenHelper類并重寫它的onCreate()、onUpgrade()方法。其中onCreate()方法會在數(shù)據(jù)庫不存在時被調(diào)用,我們可以在其中創(chuàng)建我們需要的數(shù)據(jù)庫及表;onUpgrade()方法則在數(shù)據(jù)庫版本變化時被調(diào)用,我們可以在其中升級我們的數(shù)據(jù)庫。
示例:
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. 數(shù)據(jù)庫的增刪改查
在安卓應(yīng)用中使用SQLite數(shù)據(jù)庫,需要對表中的數(shù)據(jù)進行增刪改查等操作。下面我們來看一下具體的實現(xiàn)過程。
插入數(shù)據(jù):
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();
}
刪除數(shù)據(jù):
public void delete() {
SQLiteDatabase db = helper.getWritableDatabase();
String whereClause = “id=?”;
String[] whereArgs = new String[]{“1”};
db.delete(“person”, whereClause, whereArgs);
db.close();
}
修改數(shù)據(jù):
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();
}
查詢數(shù)據(jù):
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展示數(shù)據(jù)庫數(shù)據(jù)
在應(yīng)用中使用GridView展示數(shù)據(jù)庫數(shù)據(jù),需要先通過SQLiteOpenHelper創(chuàng)建數(shù)據(jù)庫,并進行增刪改查等數(shù)據(jù)操作;然后通過BaseAdapter繼承類,實現(xiàn)自定義的Adapter并在其中設(shè)置GridView中每個Item的數(shù)據(jù)和相關(guān)屬性。
示例:
1. 定義數(shù)據(jù)項的布局文件:
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. 設(shè)置GridView中的數(shù)據(jù)和屬性:
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;
}
}
四、
相關(guān)問題拓展閱讀:
- 關(guān)于android中GridView控件
關(guān)于android中GridView控件
因為調(diào)了兩次getData(),最后你的gridView的數(shù)據(jù)源是循環(huán)加了兩遍的全局變量dataList。
方法一:刪除之一個調(diào)尺友用getData那行,不用全局變量dataList,在getData方法里面new一個局部的List,返回陵前槐這個局部變量
方法二:不刪之一個調(diào)用getData那行,new SimpleAdapter的時候不再調(diào)用getData方法,直接悔頌用dataList
android gridview數(shù)據(jù)庫的介紹就聊到這里吧,感謝你花時間閱讀本站內(nèi)容,更多關(guān)于android gridview數(shù)據(jù)庫,安卓GridView數(shù)據(jù)庫使用指南,關(guān)于android中GridView控件的信息別忘了在本站進行查找喔。
成都創(chuàng)新互聯(lián)科技有限公司,是一家專注于互聯(lián)網(wǎng)、IDC服務(wù)、應(yīng)用軟件開發(fā)、網(wǎng)站建設(shè)推廣的公司,為客戶提供互聯(lián)網(wǎng)基礎(chǔ)服務(wù)!
創(chuàng)新互聯(lián)(www.cdcxhl.com)提供簡單好用,價格厚道的香港/美國云服務(wù)器和獨立服務(wù)器。創(chuàng)新互聯(lián)成都老牌IDC服務(wù)商,專注四川成都IDC機房服務(wù)器托管/機柜租用。為您精選優(yōu)質(zhì)idc數(shù)據(jù)中心機房租用、服務(wù)器托管、機柜租賃、大帶寬租用,可選線路電信、移動、聯(lián)通等。
當(dāng)前名稱:安卓GridView數(shù)據(jù)庫使用指南(androidgridview數(shù)據(jù)庫)
網(wǎng)頁鏈接:http://www.5511xx.com/article/djsgspe.html


咨詢
建站咨詢
