在TextBox中输入数据保存并在另一个使用Intent的Activity中显示

时间:2016-01-12 05:10:35

标签: java android android-intent android-database

您好我是Android编程的新手。我想问一下如何使用intent将数据传递给另一个活动?我的情况是,我有一个2 TextBox,如果用户点击按钮,它应该被检索(文本)到数据库。

     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sqlcon = new SQLController(this);
        firstname_et = (EditText) findViewById(R.id.fistname_et_id);
        lastname_et = (EditText) findViewById(R.id.lastname_et_id);
        addmem_btn = (Button) findViewById(R.id.addmem_btn_id);
        table_layout = (TableLayout) findViewById(R.id.tableLayout1);

    BuildTable();

    addmem_btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            table_layout.removeAllViews();
            String firstname = firstname_et.getText().toString();
            String lastname = lastname_et.getText().toString();

            firstname_et.setText("");
            lastname_et.setText("");

            // inserting data
            sqlcon.open();
            sqlcon.insertData(firstname, lastname);
            BuildTable();

        }
    });
}

private void BuildTable() {

    sqlcon.open();
    Cursor c = sqlcon.readEntry();

    int rows = c.getCount();
    int cols = c.getColumnCount();

    c.moveToFirst();

    // outer for loop
    for (int i = 0; i < rows; i++) {

        TableRow row = new TableRow(this);
        row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));

        // inner for loop
        for (int j = 0; j < cols; j++) {

            TextView tv = new TextView(this);
            tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
            tv.setBackgroundResource(R.drawable.abc_btn_borderless_material);
            tv.setGravity(Gravity.CENTER);
            tv.setTextSize(18);
            tv.setPadding(0, 5, 0, 5);

            tv.setText(c.getString(j));

            row.addView(tv);

        }

        c.moveToNext();

        table_layout.addView(row);

    }
    sqlcon.close();
}

SQLitecontroller.java

public SQLController(Context c) {
    ourcontext = c;
}

public SQLController open() throws SQLException {
    dbhelper = new DatabaseHelper(ourcontext);
    database = dbhelper.getWritableDatabase();
    return this;
}

public void close() {
    dbhelper.close();
}

public void insertData(String name, String lname) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(DatabaseHelper.MEMBER_FIRSTNAME, name);
    cv.put(DatabaseHelper.MEMBER_LASTNAME, lname);
    database.insert(DatabaseHelper.TABLE_MEMBER, null, cv);

}

public Cursor readEntry() {
    // TODO Auto-generated method stub
    String[] allColumns = new String[] { DatabaseHelper.MEMBER_ID, DatabaseHelper.MEMBER_FIRSTNAME,
            DatabaseHelper.MEMBER_LASTNAME };

    Cursor c = database.query(DatabaseHelper.TABLE_MEMBER, allColumns, null, null, null,
            null, null);

    if (c != null) {
        c.moveToFirst();
    }
    return c;

}

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String TABLE_MEMBER = "member";
    public static final String MEMBER_ID = "_id";
    public static final String MEMBER_FIRSTNAME = "firstname";
    public static final String MEMBER_LASTNAME = "lastname";

    // DATABASE INFORMATION
    static final String DB_NAME = "MEMBER.DB";
    static final int DB_VERSION = 1;

    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }
    public static final String CREATE_TABLE = "create table " + TABLE_MEMBER
            + "(" + MEMBER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + MEMBER_FIRSTNAME + " TEXT NOT NULL ," + MEMBER_LASTNAME
            + " TEXT NOT NULL);";

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
    }

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

请帮帮我。 我已经尝试了很多次但会产生错误,所以请帮助我。

显示TableView中的所有数据。

0 个答案:

没有答案