2015-08-24 72 views
0

我已經創建了一個android應用程序,並且已經能夠在sqlite中創建用戶表並註冊用戶。我想以使用用戶名和密碼的用戶身份登錄到應用程序,並將其定向到他們的個人資料。現在登錄部分已完成,問題是將sqlite數據庫數據加載到用戶配置文件中。從sqlite數據庫加載用戶配置文件數據到Android用戶配置文件並編輯它

這裏的的DataHandler類

public class DataHandler extends SQLiteOpenHelper{ 
public final static String col1 = "Id"; 
public final static String col2 = "Name"; 
public final static String col3 = "Email"; 
public final static String col4 = "Mobile"; 
public final static String col5 = "Password"; 
public final static String col6 = "Address"; 
public final static String col7 = "Category"; 
public final static String col8 = "Wages_per_hour"; 
public final static String col9 = "Experience"; 
public final static String col10 = "Job_Description"; 

private static final String DB_NAME = "UtilityNp"; 
private static final int DB_VERSION = 6; 
public final static String Table_NAME = "Worker"; 

public final static String STRING_CREATE = "CREATE TABLE " + Table_NAME + " ("+col1+" INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT 1, " + col2 + " varchar(25) NOT NULL, " + col3 + " varchar(25) NOT NULL, " + col4 + " varchar(25) NOT NULL, " + col5 + " varchar(25) NOT NULL, " + col6 + " varchar(25) NOT NULL, " + col7 + " varchar(25) NOT NULL, " + col8 + " varchar(25) NOT NULL, " + col9 + " varchar(25) NOT NULL, " + col10 + " varchar(25) NOT NULL);"; 
public final static String DROP_TABLE = "DROP TABLE IF EXISTS " + Table_NAME; 

    public DataHandler(Context context) { 
    super(context, DB_NAME, null, DB_VERSION); 
} 
SQLiteDatabase db; 
@Override 
public void onCreate(SQLiteDatabase db) { 
    try { 
     db.execSQL(STRING_CREATE); 
     System.out.println("oncreate called"); 
    } catch (Exception e) { 
     System.out.println(e); 
    } 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    try { 
     db.execSQL(DROP_TABLE); 
     onCreate(db); 
     System.out.println("onupgrade called"); 
    } catch (Exception e) { 
     System.out.println(e); 
    } 
} 
public DataHandler open(){ 
    db = this.getWritableDatabase(); 
    return this; 
} 
public void close(){ 
    db.close(); 
} 
public long insertData(String a, String b, String c, String d, String e, String f, String g, String h, String i) 

{ 
    ContentValues cv = new ContentValues(); 
    cv.put(col2,a); 
    cv.put(col3,b); 
    cv.put(col4,c); 
    cv.put(col5,d); 
    cv.put(col6,e); 
    cv.put(col7,f); 
    cv.put(col8,g); 
    cv.put(col9,h); 
    cv.put(col10, i); 

    return db.insertOrThrow(Table_NAME, null, cv); 
} 

public String getSinlgeEntry(String userName) 
{ 
    Cursor cursor=db.query("Worker", null, " Name=?", new String[]{userName}, null, null, null); 
    if(cursor.getCount()<1) // UserName Not Exist 
    { 
     cursor.close(); 
     return "NOT EXIST"; 
    } 
    cursor.moveToFirst(); 
    String password= cursor.getString(cursor.getColumnIndex("Password")); 
    cursor.close(); 
    return password; 
} 

}

它的登錄類。

public class Login extends Activity { 

Button login; 
EditText editTextUserName, editTextPassword; 
DataHandler loginDataBaseAdapter; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 

    loginDataBaseAdapter=new DataHandler(this); 
    loginDataBaseAdapter.open(); 

    login =(Button)findViewById(R.id.login); 

      // get the Refferences of views 
    editTextUserName=(EditText) findViewById(R.id.uname); 
    editTextPassword= (EditText) findViewById(R.id.pass); 



    // Set On ClickListener 
    login.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // get The User name and Password 
      String userName = editTextUserName.getText().toString(); 
      String password = editTextPassword.getText().toString(); 

      // fetch the Password form database for respective user name 
      String storedPassword = loginDataBaseAdapter.getSinlgeEntry(userName); 

      if (userName.length() > 0 && password.length() > 0) { 
       if (password.equals(storedPassword)) { 

        Toast.makeText(Login.this, "Successfully Logged In", Toast.LENGTH_LONG).show(); 

          //Starting a new Intent 
          Intent nextScreen = new Intent(getApplicationContext(), User_Profile.class); 
          startActivity(nextScreen); 

       } else { 
        Toast.makeText(Login.this, "User Name or Password does not match", Toast.LENGTH_LONG).show(); 
       } 
      } else { 
       AlertDialog.Builder alertBuilder = new AlertDialog.Builder(Login.this); 
       alertBuilder.setTitle("Invalid Data"); 
       alertBuilder.setMessage("Please, Enter valid data"); 
       alertBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 

        public void onClick(DialogInterface dialog, int which) { 
         dialog.cancel(); 

        } 
       }); 
       alertBuilder.create().show(); 
      } 


     } 
    }); 


} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    // Close The Database 
    loginDataBaseAdapter.close(); 
} 

}

現在採伐後,我希望用戶獲得直接與用戶在它的信息這一活動。

這裏是xml文件。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/scrollView1" 
android:scrollbarAlwaysDrawVerticalTrack="true" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="match_parent" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="48dp" 
     android:background="#e8e8e7" 
     android:orientation="horizontal" 
     android:layout_alignParentTop="true"> 

     <TextView android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text=" Your Profile" 
      android:textColor="#2582C5" 
      android:layout_marginLeft="5dp" 
      android:textSize="23sp" 
      android:textStyle="bold" 
      android:layout_gravity="center_horizontal|center_vertical"/> 

    </LinearLayout> 
    <LinearLayout android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="16dp" 
     android:layout_marginTop="10dp" 
     android:layout_marginRight="16dp" 
     android:layout_marginBottom="30dp" 
     android:orientation="vertical" 
     android:weightSum="1"> 


     <EditText android:layout_width="fill_parent" 
      android:layout_height="40dp" 
      android:hint="Username" 
      android:background="#f3f3f3" 
      android:paddingLeft="5dp" 
      android:layout_marginTop="15dp" 
      android:id="@+id/name"/> 
     <EditText android:layout_width="fill_parent" 
      android:layout_height="40dp" 
      android:hint="Email" 
      android:background="#f3f3f3" 
      android:paddingLeft="5dp" 
      android:layout_marginTop="10dp" 
      android:id="@+id/email"/> 
     <EditText android:layout_width="fill_parent" 
      android:layout_height="40dp" 
      android:hint="Mobile" 
      android:background="#f3f3f3" 
      android:paddingLeft="5dp" 
      android:id="@+id/mobile" 
      android:layout_marginTop="10dp"/> 
     <EditText android:layout_width="fill_parent" 
      android:layout_height="40dp" 
      android:hint="Password" 
      android:background="#f3f3f3" 
      android:paddingLeft="5dp" 
      android:layout_marginTop="10dp" 
      android:id="@+id/psw"/> 
     <EditText android:layout_width="fill_parent" 
      android:layout_height="40dp" 
      android:hint="Address" 
      android:background="#f3f3f3" 
      android:paddingLeft="5dp" 
      android:layout_marginTop="10dp" 
      android:id="@+id/ad"/> 
     <EditText android:layout_width="fill_parent" 
      android:layout_height="40dp" 
      android:hint="Category" 
      android:background="#f3f3f3" 
      android:paddingLeft="5dp" 
      android:layout_marginTop="10dp" 
      android:id="@+id/cat"/> 
     <EditText android:layout_width="fill_parent" 
      android:layout_height="40dp" 
      android:hint="Wages per Hour" 
      android:background="#f3f3f3" 
      android:paddingLeft="5dp" 
      android:layout_marginTop="10dp" 
      android:id="@+id/wages"/> 
     <EditText android:layout_width="fill_parent" 
      android:layout_height="40dp" 
      android:hint="Experience" 
      android:background="#f3f3f3" 
      android:paddingLeft="5dp" 
      android:layout_marginTop="10dp" 
      android:id="@+id/experience"/> 
     <EditText android:layout_width="fill_parent" 
      android:layout_height="40dp" 
      android:hint="Job Description" 
      android:background="#f3f3f3" 
      android:paddingLeft="5dp" 
      android:layout_marginTop="10dp" 
      android:id="@+id/jdesc"/> 


     <Button 
      android:layout_width="190dp" 
      android:layout_height="40dp" 
      android:layout_marginTop="16dp" 
      android:textSize="20dp" 
      android:text="Submit" 
      android:textColor="#ffffff" 
      android:id="@+id/submit" 
      android:layout_gravity="center" 
      android:layout_weight="0.18" /> 

    </LinearLayout> 

</LinearLayout> 

如果有人能幫助我,我會心存感激。

+0

什麼是錯誤?你需要從'User_Profile'類的'onCreate()'方法中獲取數據庫中的數據。 –

回答

0

試試這個:

  • 運行SELECT語句來抓取用戶的記錄

  • 如果光標不是null意味着記錄通過遊標的OBJ

  • 通返回

  • 環每個遊標列到一個變量例如 String FName = cursor.getString(「FName Column」)

  • 將變量作爲額外內容傳遞給。將啓動UserProfile活動的內容 例如, Intent intent = new Intent(CurrentActvitiy.this,UserProfileActvitiy.class); intent.putExtra(「FName」,FName);
  • 添加完所有需要添加的數據後開始活動 例如startActvity(意向);

內的用戶配置活動onCreate方法

把進入的數據是這樣的: 意圖incomingData = getIntent(); String userFirstName = incomingData.getString(「FName」); 將檢索到的數據傳遞給Views E.g. FNameTextView.setText(userFirstName);

+0

它的工作。謝謝 –

相關問題