将用户配置文件数据从sqlite数据库加载到android中的用户配置文件并进行编辑

时间:2015-08-24 13:46:35

标签: android sqlite

我创建了一个Android应用程序,并且能够在sqlite中创建用户表并注册用户。我想以用户身份使用用户名和密码登录该应用,并转到他们的个人资料。现在登录部分已经完成,问题是将sqlite数据库数据加载到用户配置文件中。

这是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 个答案:

没有答案