2017-03-03 86 views
-2
  • 一個TextView顯示它,我已經成功地建立2代表在我SQLitesignupUploadBook我的應用程序在那裏我使用電子郵件作爲聯繫重點 之間的表和我使用 鏈接鍵創建了一個視圖,所以現在我想要從 視圖中檢索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; 
          } 
    
         } 
    

    ,在這裏我listrowhtml

    <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> 
    

回答

0

的提問是要求實現應用功能,這是過於寬泛。但是,我只能提供建議的工作流程:

您可以使用select query

select Bid, Bname, Bimage from UploadBook 

將結果保存到一個Book class

public Book{ 
    public Book(int Bid, String Bname, String Bimage){ 
    } 
} 
對象寫它檢索的所有書籍所需信息的功能

將結果傳遞給您的活動。

使用listadapter顯示這些結果在ListView

手柄Button click事件和初始化Book activity。做一個DB query檢索使用Bid和顯示效果上面一樣做這個特殊的書的信息。

相關問題