无法在我的SQLite数据库中显示我的数据[Android]

时间:2017-02-21 13:05:04

标签: android sqlite android-sqlite

我不知道出了什么问题,但是当我点击登录时,数值将不会显示。这是我的profile.java代码

    TextView vName = (TextView) findViewById(R.id.tvName);
    TextView vEmail = (TextView) findViewById(R.id.tvEmail);
    TextView vMobile = (TextView) findViewById(R.id.tvMobile);

    Contact contact = new Contact();
    vName.setText(contact.getName().toString());
    vEmail.setText(contact.getEmail().toString());
    vMobile.setText(contact.getMobile().toString());

    Button btnOut = (Button) findViewById(R.id.btnOut);
    btnOut.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent Outent = new Intent(Profile.this,MainActivity.class);
            startActivity(Outent);
            finish();
        }
    });

这是我的数据库,我尝试在我的DatabaseHelper.java中创建一个方法,但它仍然无效。我想得到像你们这样的专业人士的帮助。 DatabaseHelper.java

private static final String TABLE_CREATE = "create table contacts(id integer primary key not null ,"+
        "name text not null, email text not null, pass text not null, mobile text not null);";

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


@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_CREATE);
    this.db =db;
}

public void insertContact(Contact c){
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    String query = "select * from contacts";
    Cursor cursor = db.rawQuery(query,null);
    int count = cursor.getCount();


    values.put(COLUMN_ID,count);
    values.put(COLUMN_NAME,c.getName());
    values.put(COLUMN_EMAIL,c.getEmail());
    values.put(COLUMN_PASS, c.getPass());
    values.put(COLUMN_MOBILE, c.getMobile());

    db.insert(TABLE_NAME,null,values);
    db.close();
}

public Cursor getAllData(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select email,name,mobile from"+TABLE_NAME,null);
    return res;
}

public String searchPass(String email){
    db = this.getReadableDatabase();
    String query = "select email, pass from " + TABLE_NAME;
    Cursor cursor = db.rawQuery(query,null);
    String a, b;
    b = "not found";
    if(cursor.moveToFirst()){
        do{
            a = cursor.getString(0);

            if(a.equals(email)){
                b = cursor.getString(1);
                break;
            }
        }while(cursor.moveToNext());
    }
    return b;
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String query = "DROP TABLE IF EXISTS" + TABLE_NAME;
    db.execSQL(query);
    this.onCreate(db);
}

1 个答案:

答案 0 :(得分:0)

您未在个人资料活动中致电getAllData()

<强> ProfileActivity

 List<Contact> contacts = db.getAllData();          
        for (Contact cn : contacts) {
            String name = cn.getName();
            vName.setText(name);
            // follow by getEmail and getMobile
        }

<强> DatabaseHelper

 // Getting All Data
    public List<Contact> getAllData() {
        List<Contact> contactList = new ArrayList<Contact>();
        // Select All Query
        String selectQuery = "SELECT email,name,mobile from"+TABLE_NAME;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Contact contact = new Contact();
                contact.setName((cursor.getString(0));
                contact.setEmail(cursor.getString(1));
                contact.setMobile(cursor.getString(2));
                contactList.add(contact);
            } while (cursor.moveToNext());
        }

        return contactList;
    }

希望它准确而有用。