I'm using an SQLite database to hold user information such as Username password age gender height etc.
The database seems to be working fine (no errors or exceptions while debugging) however when I go to retrive a Single user and his/her details the app just stalls after the button is clicked. I have a databasehandler java file with the method for a single user retrieval and a Registerdetails file with the setter and getter methods.
The button has an onclicklistner set to a method called Userinfo(). THe user interface has an edittext box to enter the id, a button and a textview to display the results.
My question is is my query wrong or is my database?
RegisterDetails.java
public class Registerdetails {
int _id;
String _user_name;
String _password;
String _gender;
double _height;
int _age;
public Registerdetails(){}
public Registerdetails(int _id, String _user_name,String _password, String _gender, double _height, int _age){
this._user_name = _user_name;
this._password = _password;
this._age = _age;
this._gender = _gender;
this._height = _height;
this._id = _id;
}
public int getID(){
return this._id;
}
public void setID(int id){
this._id = id;
}
public String get_user_name(){
return this._user_name;
}
public void set_user_name(String user_name){
this._user_name = user_name;
}
public String get_password(){
return this._password;
}
public void set_password(String password){
this._password = password;
}
public String get_gender(){
return this._gender;
}
public void set_gender(String gender){
this._user_name = gender;
}
public int get_age(){
return this._age;
}
public void set_age(int age){
this._age = age;
}
public double get_height(){
return this._height;
}
public void set_height(double height){
this._height = height;
}
}
DatabaseHandler.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.john.databasetest.Product;
import com.example.john.databasetest.Registerdetails;
public class DatabaseHandler extends SQLiteOpenHelper {
//DATABASE VERSION
private static final int DATABASE_VERSION = 1;
//DATABASE NAME
private static final String DATABASE_NAME = "Appdatabase";
//TABLE NAMES
private static final String TABLE_REGISTER = "Register_Details";
//REGISTER COLUMN NAMES
private static final String KEY_USERID = "UserID";
private static final String KEY_USERNAME = "Username";
private static final String KEY_PASSWORD = "Password";
private static final String KEY_AGE = "Age";
private static final String KEY_GENDER = "Gender";
private static final String KEY_HEIGHT = "Height";
public DatabaseHandler(Context context){
super(context,DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db){
String CREATE_REGISTERDETAILS_TABLE = "CREATE TABLE " + TABLE_REGISTER + " ( "+ KEY_USERID + " INTEGER PRIMARY KEY , " + KEY_USERNAME + " TEXT, " +
KEY_PASSWORD + " TEXT, " +
KEY_GENDER + " TEXT, " +
KEY_HEIGHT + " DOUBLE, " +
KEY_AGE + " INTEGER, " +
);";
//CREATING REQUIRED TABLES
db.execSQL(CREATE_REGISTERDETAILS_TABLE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
//ON UPGRADE DROP THE OLDER TABLES
db.execSQL("DROP TABLE IF EXISTS" + TABLE_REGISTER);
onCreate(db);
}
//query for retrieving a single register
Registerdetails get_userinfo (int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_REGISTER, new String[] { KEY_USERID,
KEY_USERNAME, KEY_PASSWORD }, KEY_USERID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if(cursor != null)
cursor.moveToFirst();
Registerdetails user_info = new Registerdetails(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2),cursor.getString(3), cursor.getDouble(4),
cursor.getDouble(5));
cursor.close();
return user_info;
}
}
Home.java
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import java.util.List;
public class Home extends ActionBarActivity {
Button btn;
TextView dbtxt;
EditText idedit;
int id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
btn = (Button) findViewById(R.id.btndb);
dbtxt = (TextView) findViewById(R.id.txtdb);
DatabaseHandler db = new DatabaseHandler(this);
Log.d("Inserting: ", "Inserting ..");
db.addRegistered_user(new Registerdetails(0, "steve", "2345", "Male", 182.0, 21));
db.addRegistered_user(new Registerdetails(1, "kevin", "1234","Male", 186.0,20));
db.addRegistered_user(new Registerdetails(2, "paul", "5555","Male", 178.0, 86.0, 87.5, 1.5,20,0.0,0.0,0.0,0.0,0.0,0.0,0.0, 600.00));
}
public void Userinfo(){
idedit = (EditText) findViewById(R.id.IDedit);
id = Integer.parseInt(idedit.getText().toString());
DatabaseHandler db = new DatabaseHandler(this);
Registerdetails user = db.get_userinfo(id);
String Log = "Id: " + user.getID() + "\nUsername: " + user.get_user_name() + "Password: " + user.get_password();
dbtxt.setText(Log);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Main_activity.xml
<TextView
android:layout_width="300dp"
android:layout_height="80dp"
android:id="@+id/txtdb"
android:backgroundTint="#ff75ff2f"
android:text="Results"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textAlignment="center" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="PRESS"
android:id="@+id/btndb"
android:onClick="Viewuserinfo"
android:layout_below="@+id/txtdb"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/IDedit"
android:layout_above="@+id/txtdb"
android:layout_centerHorizontal="true"
android:layout_marginBottom="82dp" />
Aucun commentaire:
Enregistrer un commentaire