栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

android studio从Sqlite数据库检索数据并将其显示为textview

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android studio从Sqlite数据库检索数据并将其显示为textview

这是我如何实现此目标的示例。

在这个例子中,我会

store
retrieve
update
delete
一个学生的名字和年龄。


首先创建一个类,我叫我的

DBManager.java

public class DBManager {    private Context context;    private SQLiteDatabase database;    private SQLiteHelper dbHelper;    public DBManager(Context c) {        this.context = c;    }    public DBManager open() throws SQLException {        this.dbHelper = new SQLiteHelper(this.context);        this.database = this.dbHelper.getWritableDatabase();        return this;    }    public void close() {        this.dbHelper.close();    }    public void insert(String name, String desc) {        ContentValues contentValue = new ContentValues();        contentValue.put(SQLiteHelper.NAME, name);        contentValue.put(SQLiteHelper.AGE, desc);        this.database.insert(SQLiteHelper.TABLE_NAME_STUDENT, null, contentValue);    }    public Cursor fetch() {        Cursor cursor = this.database.query(SQLiteHelper.TABLE_NAME_STUDENT, new String[]{SQLiteHelper._ID, SQLiteHelper.NAME, SQLiteHelper.AGE}, null, null, null, null, null);        if (cursor != null) { cursor.moveToFirst();        }        return cursor;    }    public int update(long _id, String name, String desc) {        ContentValues contentValues = new ContentValues();        contentValues.put(SQLiteHelper.NAME, name);        contentValues.put(SQLiteHelper.AGE, desc);        return this.database.update(SQLiteHelper.TABLE_NAME_STUDENT, contentValues, "_id = " + _id, null);    }    public void delete(long _id) {        this.database.delete(SQLiteHelper.TABLE_NAME_STUDENT, "_id=" + _id, null);    }}

然后创建一个

SQLiteOpenHelper
我叫我的

SQLiteHelper.java

public class SQLiteHelper extends SQLiteOpenHelper {    public static final String AGE = "age";    private static final String CREATE_TABLE_STUDENT = " create table STUDENTS ( _id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL , age TEXT );";    private static final String DB_NAME = "STUDENTS.DB";    private static final int DB_VERSION = 1;    public static final String NAME = "name";    public static final String TABLE_NAME_STUDENT = "STUDENTS";    public static final String _ID = "_id";    public SQLiteHelper(Context context) {        super(context, DB_NAME, null, 1);    }    public void onCreate(SQLiteDatabase db) {        db.execSQL(CREATE_TABLE_STUDENT);    }    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {        db.execSQL("DROP TABLE IF EXISTS STUDENTS");        onCreate(db);    }   }

添加:

在此示例中,我取自文本,

EditText
单击按钮时,我检查是否
EditText
为空。如果它不为空并且该学生不存在,则将学生的姓名和年龄插入数据库中。我显示一个
Toast
,让用户知道状态:

btnAdd.setonClickListener(new View.onClickListener() {    @Override    public void onClick(View view) {        if (edtName.getText().toString().trim().length() == 0) { Toast.makeText(getApplicationContext(), "Please provide your students name", Toast.LENGTH_SHORT).show();        } else{ try {     if (edtAge.getText().toString().trim().length() != 0) {         String name = edtName.getText().toString().trim();         String age = edtAge.getText().toString().trim();         String query = "Select * From STUDENTS where name = '"+name+"'";         if(dbManager.fetch().getCount()>0){  Toast.makeText(getApplicationContext(), "Already Exist!", Toast.LENGTH_SHORT).show();         }else{  dbManager.insert(name, age);  Toast.makeText(getApplicationContext(), "Added successfully!", Toast.LENGTH_SHORT).show();   }     } else {         Toast.makeText(getApplicationContext(), "please provide student age!", Toast.LENGTH_SHORT).show();          } } catch (Exception e) {     e.printStackTrace(); }        }    }  });

更新:

在这里,我将接受“文本”

EditText
并在单击按钮时更新学生。您也可以将以下内容放在中
try/catch
,以确保更新成功。

btnupdate.setonClickListener(new View.onClickListener() {    @Override    public void onClick(View view) {        String name = nameText.getText().toString();        String age = ageText.getText().toString();        dbManager.update(_id, name, age);        Toast.makeText(getApplicationContext(), "Updated successfully!", Toast.LENGTH_SHORT).show();    }});

删除:

dbManager.delete(_id);Toast.makeText(getApplicationContext(), "Deleted successfully!", Toast.LENGTH_SHORT).show();

要得到:

在这里,我得到了学生的名字,并将其显示在

TextView

DBManager dbManager = new DBManager(getActivity());dbManager.open();Cursor cursor = dbManager.fetch();cursor.moveToFirst();final TextView studentName = (TextView) getActivity().findViewById(R.id.nameOfStudent);studentName.settext(cursor.getString(0));


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/425884.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号