从现有数据库中检索数据并将其显示在android中的TextView中

时间:2017-03-03 16:36:43

标签: android sqlite

  • 我已在SQLite signupUploadBook成功构建了2个表格 我的应用image,Bname,BId,其中我使用电子邮件作为链接密钥 表和我使用两个表创建了一个视图 链接键所以现在我想从中检索BId 查看以在列表视图中显示所有书籍,然后在用户单击时显示 该按钮将加载到另一个活动并显示所有 有关使用SQLite(图书ID)的图书的信息。

    这是我的 public class DBConnection extends SQLiteOpenHelper { public static final String DbName="RFAll.db"; SQLiteDatabase db; public DBConnection(Context context){ super(context,DbName,null,1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table IF NOT EXISTS UploadBook(BId INTEGER PRIMARY KEY AUTOINCREMENT,Bname TEXT,Bcategory TEXT,AuthorName TEXT,Bdecs TEXT, Brate REAL, country TEXT, city TEXT, address TEXT, phoneno INTEGER, Email TEXT, Bprice REAL, Brent REAL, Bdeposit REAL, Bimage BLOB ,FOREIGN KEY (Email) REFERENCES signup(Email))"); db.execSQL("create table IF NOT EXISTS signup(ID INTEGER PRIMARY KEY AUTOINCREMENT,Name TEXT,Email TEXT,Password TEXT,REpassword TEXT)"); db.execSQL("create VIEW IF NOT EXISTS show as select BId,Bname,Bcategory,AuthorName,Bdecs,Brate,country,city,address,phoneno,Bprice,Brent,Bdeposit,Bimage,ID,signup.Email from signup left join UploadBook on signup.Email=UploadBook.Email"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("Drop table if EXISTS UploadBook"); db.execSQL("DROP TABLE IF EXISTS signup"); onCreate(db); } public void insertRow(String Name,String Email,String Password,String REpassword) { SQLiteDatabase db=this.getWritableDatabase(); ContentValues contentValues= new ContentValues(); contentValues.put("Name", Name); contentValues.put("Email", Email); contentValues.put("Password", Password); contentValues.put("REpassword", REpassword); db.insert("signup",null,contentValues); } public void InsertRowUpload(String Bname,String Bcategory,String AuthorName,String Bdecs,Float Brate,String country,String city,String address,Double Phoneno,String Email,Float Bprice,Float Brent ,Float Bdeposit,byte [] Bimage ) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put("Bname", Bname); contentValues.put("Bcategory", Bcategory); contentValues.put("AuthorName", AuthorName); contentValues.put("Bdecs", Bdecs); contentValues.put("Brate", Brate); contentValues.put("country", country); contentValues.put("city", city); contentValues.put("address", address); contentValues.put("Phoneno", Phoneno); contentValues.put("Email", Email); contentValues.put("Bprice", Bprice); contentValues.put("Brent", Brent); contentValues.put("Bdeposit", Bdeposit); contentValues.put("Bimage", Bimage); db.insert("UploadBook",null,contentValues); } public void close() { db.close(); } public boolean getUser(String email, String pass) { String selectQuery = "SELECT * FROM signup WHERE Email= '"+ email+"' AND Password = '"+ pass+"' "; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); // Move to first row cursor.moveToFirst(); if (cursor.getCount() > 0) { return true; } cursor.close(); db.close(); return false; } public boolean getEmail(String email) { String selectQuery = "SELECT * FROM signup WHERE Email= '"+ email+"' "; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); // Move to first row cursor.moveToFirst(); //if (cursor != null) if (cursor.getCount() > 1) { return true; } cursor.close(); db.close(); return false; } public boolean getAdmin(String email,String pass) { String selectQuery = "SELECT * FROM signup WHERE ID=2 AND Email= '"+ email+"' AND Password = '"+ pass+"' "; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); // Move to first row cursor.moveToFirst(); if (cursor.getCount() > 0) { return true; } cursor.close(); db.close(); return false; } }

    listrow

    此处我的html <ImageView android:layout_width="100dp" android:layout_height="100dp" app:srcCompat="@mipmap/ic_launcher" android:id="@+id/imageView" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <TextView android:text="TextView" android:layout_width="85dp" android:layout_height="wrap_content" android:id="@+id/txtcost" android:layout_marginBottom="21dp" android:layout_alignBottom="@+id/imageView" android:layout_alignLeft="@+id/txttitle" android:layout_alignStart="@+id/txttitle" /> <TextView android:text="TextView" android:layout_width="85dp" android:layout_height="wrap_content" android:id="@+id/txttitle" android:layout_marginLeft="16dp" android:layout_marginStart="16dp" android:layout_above="@+id/txtcost" android:layout_toRightOf="@+id/imageView" android:layout_toEndOf="@+id/imageView" /> <Button android:text="MORE" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/butget" android:layout_alignBottom="@+id/txtcost" android:layout_toRightOf="@+id/txtcost" android:layout_toEndOf="@+id/txtcost" android:layout_marginLeft="14dp" android:layout_marginStart="14dp" /> </RelativeLayout>

        

    listview

    和我的<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.karman.first.MainActivity"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/ListView" android:layout_above="@+id/button" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout>

    SubpanelQuickCreate

1 个答案:

答案 0 :(得分:0)

提问者要求实施太广泛的应用功能。但是,我只能提供建议的工作流程:

您可以编写一个函数,使用select query

检索所有图书的必需信息
select Bid, Bname, Bimage from UploadBook

将结果存储在Book class

的对象中
public Book{
  public Book(int Bid, String Bname, String Bimage){
  }
}

将结果传递给您的活动。

使用listadapterListView

中显示这些结果

处理Button click事件并初始化Book activity。设DB query使用Bid检索此特定图书的信息,并显示与上述相同的结果。