-2
一個TextView顯示它,我已經成功地建立2代表在我
SQLite
爲signup
和UploadBook
我的應用程序在那裏我使用電子郵件作爲聯繫重點 之間的表和我使用 鏈接鍵創建了一個視圖,所以現在我想要從 視圖中檢索image,Bname,BId
以在列表視圖中顯示所有書籍,然後當用戶單擊 時,該按鈕將加載到另一個活動並顯示所有有關使用BId
(書ID)的書 信息。從現有的數據庫中檢索數據,並在Android的這裏我
SQLite
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>