2016-10-28 62 views
-1

目前我正在爲我在大學的最後一堂課開發一款應用程序。該應用程序的概念是一個聯繫人保持應用程序,本質上,用戶將通過登錄屏幕迎接,他們可以登錄,如果他們已經有一個帳戶或註冊,如果他們不。當你註冊用戶時會提示輸入他們的姓名,用戶名,密碼和電子郵件。然後他們提交它們,他們將再次被登錄屏幕迎接,他們現在可以用他們最近創建的憑證登錄。一旦他們成功登錄,他們就會顯示一個活動,通過匹配登錄名上的用戶名和密碼,從數據庫中的表中檢索他們的名字來歡迎他們,它將檢索該行中的所有信息。我幾乎沒有這樣做,但是我遇到麻煩的是我要在哪裏保存用戶想要保存的聯繫人,我知道這是必須在另一個表中,但我將如何設置它,所以我可以將某個用戶與其他表中的所有聯繫人進行匹配。如何爲Android Studio應用程序設置MySQL數據庫表?

一旦我得到這些信息,我需要把它放在一個ListView中,這樣用戶可以看到他們的聯繫人並滾動瀏覽它們,然後他們可以添加,刪除或編輯它們。

截至目前,我能夠註冊,登錄和顯示歡迎屏幕,沒有問題,現在我只需要能夠添加,刪除和編輯聯繫人。

Java用於Android Studio部分,PHP用於使主機在線連接的文件。

我正在使用Volley和JSON請求。

向數據庫發出請求的數據庫和PHP文件託管在000webhost中。

+0

您可以從這裏開始[Android CRUD教程](https://www.codeofaninja.com/2013/02/android-sqlite-tutorial。html) –

+0

聽起來像一個艱難的。祝你好運! 你能不能僅僅爲我們提供一點作業?你已經嘗試了什麼?我們可以看到你的代碼,以便其他人可以修補它嗎? – mike510a

+0

我需要關於如何在數據庫上設置表的幫助。表1用戶:id,姓名,用戶名,密碼,電子郵件字段。然後我有表2聯繫人:id,user_id,名字,姓氏,電話,電子郵件。我想知道如何匹配這兩個表,我認爲與user_id,但我不知道我是否在正確的軌道 –

回答

0

這看起來像一個簡單的CRUD應用程序。 如果您使用RESTful通信模型,實現起來相當簡單。 至於存儲聯繫人,可以爲每個用戶指定一個文件,並在DB中的用戶表上具有該文件的URI。該文件可以是CSV或JSON,可以通過正確的API輕鬆轉換爲POJO。

祝你好運。

-1

首先,創建一個MySQLite Helper類

public class MySQLiteHelper extends SQLiteOpenHelper { 
public static final String TABLE_CONTACTS = "contacts"; 
public static final String COLUMN_CONTACTS_ID = "_id"; 
public static final String COLUMN_NAME = "name"; 
public static final String COLUMN_USERNAME ="username"; 
public static final String COLUMN_PASSWORD = "password" 

private static final String DATABASE_NAME = "Contacts.db"; 
private static final int DATABASE_VERSION = 1; 

private static final String DATABASE_CONTACTS_CREATE = "create table "+ TABLE_CONTACTS+ 
     "("+COLUMN_CONTACTS_ID+" integer primary key autoincrement, " 
     +COLUMN_NAME+" text not null, " 
     +COLUMN_USERNAME+" text not null," 
     +COLUMN_PASSWORD+" text not null);"; 


public MySQLiteHelper(Context context){ 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

@Override 
public void onCreate(SQLiteDatabase database){ 
    database.execSQL(DATABASE_CONTACTS_CREATE); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ 
    Log.w(MySQLiteHelper.class.getName(),"Upgrading database from version " 
      +oldVersion 
      + " to" 
      +newVersion 
      +", which will destroy all old data"); 

    db.execSQL("DROP TABLE IF EXISTS "+TABLE_CONTACTS); 

    onCreate(db); 
}} 

然後一個模型類

public class ContactModel { 
private long id; 
private String name; 
private String username; 
private String password; 
/////////// 
public long getId(){ 
    return id; 
} 
public void setId(long id){ 
    this.id = id; 
} 
////////// 
public String getName(){ 
    return name; 
} 
public void setName(String name){ 
    this.name = name; 
} 
public void setUsername(String username){ 
    this.username = username; 
} 
//////////// 
public String getPassword(){ 
    return password; 
} 
public void setPassword(String password){ 
    this.password = password; 
} } 

然後一個數據源

public class ContactsDataSource { 

private SQLiteDatabase database; 
private MySQLiteHelper dbHelper; 
private String[] allContactsColumns = {MySQLiteHelper.COLUMN_CONTACTS_ID,MySQLiteHelper.COLUMN_NAME,MySQLiteHelper.COLUMN_USERNAME, 
     MySQLiteHelper.COLUMN_PASSWORD}; 

public ContactsDataSource(Context context){ 
    dbHelper = new MySQLiteHelper(context); 
} 

public void open() throws SQLException { 
    database = dbHelper.getWritableDatabase(); 
} 

public void close(){ 
    dbHelper.close(); 
} 


public ContactModel createContact(String name, String username, String password){ 
    ContentValues values = new ContentValues(); 
    values.put(MySQLiteHelper.COLUMN_NAME, name); 
    values.put(MySQLiteHelper.COLUMN_EMAIL,username); 
    values.put(MySQLiteHelper.COLUMN_PHONE,password); 
    long insertContactsId = database.insert(MySQLiteHelper.TABLE_CONTACTS, null, values); 
    Cursor cursor = database.query(MySQLiteHelper.TABLE_CONTACTS,allContactsColumns,MySQLiteHelper.COLUMN_CONTACTS_ID+" = " 
      +insertContactsId, null, null, null, null); 
    cursor.moveToFirst(); 
    ContactModel newContact = cursorToContact(cursor); 
    cursor.close(); 
    Log.d("written", "Info was written"); 
    return newContact; 
} 


public void deleteContact(ContactModel contact){ 
    long id = contact.getId(); 
    System.out.println("Comment deleted with id: "+id); 
    database.delete(MySQLiteHelper.TABLE_CONTACTS,MySQLiteHelper.COLUMN_CONTACTS_ID 
      +" = "+id,null); 
} 


public ArrayList<ContactModel> getAllContacts(){ 
    ArrayList<ContactModel> contacts = new ArrayList<>(); 
    Cursor cursor = database.query(MySQLiteHelper.TABLE_CONTACTS, allContactsColumns, null, null, null, null, null); 

    cursor.moveToFirst(); 
    while (!cursor.isAfterLast()){ 
     ContactModel contact = cursorToContact(cursor); 
     contacts.add(contact); 
     cursor.moveToNext(); 
    } 
    cursor.close(); 
    return contacts; 
} 

private ContactModel cursorToContact(Cursor cursor){ 
    ContactModel contact = new ContactModel(); 
    contact.setId(cursor.getLong(0)); 
    contact.setName(cursor.getString(1)); 
    contact.setUsername(cursor.getString(2)); 
    contact.setPassword(cursor.getString(3)); 
    return contact; 
}} 

您訪問數據源如下:

您可以通過調用ContactsDataSource.getAllContacts()來顯示列表視圖中的信息,並使用自定義listAdapter顯示結果。這個想法是,你將使用數據源的函數來獲取你想要的數據,並將它提供給模型類。從那裏你可以提取你需要的listview。讓我知道你是否需要更多的幫助。

您還提到想要在數據庫中創建多個表,這與使用上面的代碼一樣簡單,但只需添加另一個變量(如TABLE_CONTACTS2 =「contacts2」)並複製該表的函數即可。

相關問題