从数据库中检索数据并放入textview

时间:2017-11-14 05:45:42

标签: java android android-studio

我成功地将数据插入到数据库中,但是当我尝试检索数据并在textView中显示时,它会被定向到MainActivity。

Regiser_page.java如下

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="no.nordicsemi.android.nrftoolbox.Register_Page">

    <RelativeLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:orientation="vertical"
        android:weightSum="1">

        <TextView
            android:id="@+id/register"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/editText_name"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="100dp"
            android:layout_marginTop="28dp"
            android:layout_weight="0.36"
            android:text="Register Here"
            android:textAlignment="center"
            android:textAllCaps="true"
            android:textColor="#3333ff"
            android:textSize="28dp" />

        <Button
            android:id="@+id/button_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="790dp"
            android:onClick="mydata"
            android:text="View data" />

        <TextView
            android:id="@+id/textView_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14sp"
            android:layout_marginTop="70sp"
            android:autoText="false"
            android:text="Name :"
            android:textColor="#3368ff"
            android:textColorLink="#000000"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/editText_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10sp"
            android:layout_marginRight="10sp"
            android:layout_marginTop="100sp"
            android:ems="10"
            android:hint="Enter your name"
            android:inputType="textPersonName" />

        <TextView
            android:id="@+id/textView_email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14sp"
            android:layout_marginTop="160sp"
            android:text="Email-id :"
            android:textColor="#3368ff"
            android:textColorLink="#000000"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/editText_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10sp"
            android:layout_marginRight="10sp"
            android:layout_marginTop="190sp"
            android:ems="10"
            android:hint="Enter Email-id"
            android:inputType="textEmailAddress" />

        <TextView
            android:id="@+id/textView_pass"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14dp"
            android:layout_marginTop="250dp"
            android:hint="Enter Password"
            android:text="Password"
            android:textColor="#3368ff"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/editText_pass"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="280dp"
            android:ems="10"
            android:hint="Enter Password"
            android:inputType="textPassword" />

        <TextView
            android:id="@+id/textView_dob"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14sp"
            android:layout_marginTop="450sp"
            android:text="Date of Birth :"
            android:textColor="#3368ff"
            android:textColorLink="#000000"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/editText_dob"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="480dp"
            android:ems="10"
            android:hint="Date of Birth"
            android:inputType="date" />

        <TextView
            android:id="@+id/textView_phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14sp"
            android:layout_marginTop="540sp"
            android:text="Phone No. :"
            android:textColor="#3368ff"
            android:textColorLink="#000000"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/editText_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10sp"
            android:layout_marginRight="10sp"
            android:layout_marginTop="570sp"
            android:ems="10"
            android:hint="Enter Phone No."
            android:inputType="phone" />

        <TextView
            android:id="@+id/textView_city"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14sp"
            android:layout_marginTop="630sp"
            android:text="City :"
            android:textColor="#3368ff"
            android:textColorLink="#000000"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/editText_city"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10sp"
            android:layout_marginRight="10sp"
            android:layout_marginTop="660sp"
            android:ems="10"
            android:hint="Enter your City"
            android:inputType="textPersonName" />

        <TextView
            android:id="@+id/textView_gender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14sp"
            android:layout_marginTop="340sp"
            android:text="Gender :"
            android:textColor="#3368ff"
            android:textColorLink="#000000"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/editText_gender"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="370dp"
            android:ems="10"
            android:hint="M/F/Others"
            android:inputType="textPersonName" />

        <Button
            android:id="@+id/button_register"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="110dp"
            android:layout_marginTop="720dp"
            android:background="#3368ff"
            android:onClick="addUser"
            android:text="Register"
            android:textAlignment="center"
            android:textColor="#ffffff" />

    </RelativeLayout>
</android.support.v4.widget.NestedScrollView>

Activity_register_page.xml如下:

public class View_Profile extends AppCompatActivity {
    TextView name, email, pass, phone, gender, city, dob;
    int id_To_Update=0;
    private no.nordicsemi.android.nrftoolbox.myDbAdapter mydb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view__profile);
        name = (TextView) findViewById(R.id.textView_name);
        phone = (TextView) findViewById(R.id.textView_phone);
        email = (TextView) findViewById(R.id.textView_email);
        pass = (TextView) findViewById(R.id.textView_pass);
        gender = (TextView) findViewById(R.id.textView_gender);
        dob = (TextView) findViewById(R.id.textView_dob);
        city = (TextView) findViewById(R.id.textView_city);
        mydb = new no.nordicsemi.android.nrftoolbox.myDbAdapter(this);
        Cursor rs = mydb.getData(1);
        id_To_Update = 1;
        rs.moveToFirst();

        String nam = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_NAME));
        String phon = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_PHONE));
        String emai = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_EMAIL));
        String cit = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_CITY));
        String gend = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_GENDER));
        String pas = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_PASS));
        String dobb = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_DOB));

        if (!rs.isClosed())  {
            rs.close();
        }


        name.setText((CharSequence)nam);
        name.setFocusable(false);
        name.setClickable(false);

        email.setText((CharSequence)emai);
        email.setFocusable(false);
        email.setClickable(false);

        pass.setText((CharSequence)pas);
        pass.setFocusable(false);
        pass.setClickable(false);

        dob.setText((CharSequence)dobb);
        dob.setFocusable(false);
        dob.setClickable(false);

        gender.setText((CharSequence)gend);
        gender.setFocusable(false);
        gender.setClickable(false);

        phone.setText((CharSequence)phon);
        phone.setFocusable(false);
        phone.setClickable(false);

        city.setText((CharSequence)cit);
        city.setFocusable(false);
        city.setClickable(false);

    }
}

View_Profile.java如下:

public class myDbAdapter extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "MyDBName.db";
    public static final String CONTACTS_TABLE_NAME = "contacts";
    public static final String CONTACTS_COLUMN_ID = "id";
    public static final String CONTACTS_COLUMN_NAME = "name";
    public static final String CONTACTS_COLUMN_EMAIL = "email";
    public static final String CONTACTS_COLUMN_PASS = "password";
    public static final String CONTACTS_COLUMN_DOB = "date-of-birth";
    public static final String CONTACTS_COLUMN_GENDER = "gender";
    public static final String CONTACTS_COLUMN_PHONE="phone";
    public static final String CONTACTS_COLUMN_CITY="city";
    private HashMap hp;

    public myDbAdapter(Context context) {
        super(context, DATABASE_NAME , null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(
                "create table contacts " +
                        "(id integer primary key, name text,email text,pass text, dob text,gender text,phone text,city text)"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS contacts");
        onCreate(db);
    }

    public boolean insertContact (String name, String email, String pass, String dob,String gender,String phone,String city) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", name);
        contentValues.put("email", email);
        contentValues.put("password", pass);
        contentValues.put("date-of-birth", dob);
        contentValues.put("gender", gender);
        contentValues.put("phone",phone);
        contentValues.put("city",city);
        db.insert("contacts", null, contentValues);
        return true;
    }

    public Cursor getData(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res =  db.rawQuery( "select * from contacts where id="+id+"", null );

        return res;
    }

    public int numberOfRows(){
        SQLiteDatabase db = this.getReadableDatabase();
        int numRows = (int) DatabaseUtils.queryNumEntries(db, CONTACTS_TABLE_NAME);
        return numRows;
    }

    public boolean updateContact (Integer id, String name, String email, String pass, String dob,String gender,String phone,String city) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", name);
        contentValues.put("email", email);
        contentValues.put("password", pass);
        contentValues.put("date-of-birth", dob);
        contentValues.put("gender", gender);
        contentValues.put("phone",phone);
        contentValues.put("city",city);
        db.update("contacts", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
        return true;
    }

    public Integer deleteContact (Integer id) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete("contacts",
                "id = ? ",
                new String[] { Integer.toString(id) });
    }

    public ArrayList<String> getAllCotacts() {
        ArrayList<String> array_list = new ArrayList<String>();

        //hp = new HashMap();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res =  db.rawQuery( "select * from contacts", null );
        res.moveToFirst();

        while(res.isAfterLast() == false){
            array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));
            res.moveToNext();
        }
        return array_list;
    }
}

myDbAdapter.java如下:

near "-": syntax error
 Error inserting phone=5895346896 email=man@gmail.com name=my name gender=male password=47ufvjtio city=bangalore date-of-birth=30-02-95
                                                                                  android.database.sqlite.SQLiteException: near "-": syntax error (code 1): , while compiling: INSERT INTO contacts(phone,email,name,gender,password,city,date-of-birth) VALUES (?,?,?,?,?,?,?)
                                                                                      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                                      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                                      at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                      at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                                      at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                                      at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
                                                                                      at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
                                                                                      at no.nordicsemi.android.nrftoolbox.myDbAdapter.insertContact(myDbAdapter.java:60)
                                                                                      at no.nordicsemi.android.nrftoolbox.Register_Page.addUser(Register_Page.java:194)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                      at android.view.View$1.onClick(View.java:4020)
                                                                                      at android.view.View.performClick(View.java:4785)
                                                                                      at android.view.View$PerformClick.run(View.java:19888)
                                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:135)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5276)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)

将View_Profile定向到MainActivity并导致结果显示失败的错误是什么?

我的日志文件显示以下错误:

{{1}}

insertContact代码中是否有任何错误?或者为什么这不起作用?

2 个答案:

答案 0 :(得分:0)

添加新活动时,您必须在清单文件中注册它。您应该在项目清单文件中注册新活动。

 <activity
        android:name=".Register_Page"/>

 <activity
        android:name=".View_Profile"/>

在项目的清单文件中添加这两行代码。您必须在MainActivity的标记在清单文件中结束后添加它们。

答案 1 :(得分:0)

让我们说清楚:

***您正在创建包含“dob”列

的表格
 @Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(
            "create table contacts " +
                    "(id integer primary key, name text,email text,pass text, dob text,gender text,phone text,city text)"
    );
}

***现在看看

public boolean insertContact (String name, String email, String pass, String dob,String gender,String phone,String city) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("name", name);
    contentValues.put("email", email);
    contentValues.put("password", pass);
    //contentValues.put("date-of-birth", dob);
    // it should be "dob" instead of "date-of-birth"
    contentValues.put("dob", dob);
    // like this
    contentValues.put("gender", gender);
    contentValues.put("phone",phone);
    contentValues.put("city",city);
    db.insert("contacts", null, contentValues);
    return true;
}

**也在这里做出改变

// public static final String CONTACTS_COLUMN_DOB = "date-of-birth";
 // as 
public static final String CONTACTS_COLUMN_DOB = "dob";

所有关于出生日期的列名称不匹配

相关问题