0

Hello I'm new to android programming. I would like to ask how can I pass data to another activity using intent? My case here is, I have a 2 TextBox , if the user clicks on the button it should be retrieved(the text) to the database.

     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);
    }
}

please help me. i have try many time but error will generated so please help me.

show all data in TableView.

Aditya Vyas-Lakhan
  • 12,393
  • 15
  • 55
  • 92
  • Please add the errors you are getting and identify the corresponding code. – ChiefTwoPencils Jan 12 '16 at 05:12
  • Passing data: http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android – CmosBattery Jan 12 '16 at 05:22
  • Try to see if you can create a smaller example ([mcve]) to highlight your point. People are more likely to read and respond (and not go TL;DR) if you can create a small well-formatted example. – Praveen Jan 12 '16 at 05:45
  • do you want to pass it to the another activity or save it in the database.? specify clearly. – Ragu Swaminathan Jan 12 '16 at 07:04
  • what issue you facing?? – Aditya Vyas-Lakhan Jan 12 '16 at 11:29
  • if you just need to display the data in next activity without storing, this tutorial about passing data using intent will help you http://www.vogella.com/tutorials/AndroidIntent/article.html . If you want to store data use sharedpreference . Explain the need to store data, so that i can tell you whether you need to implement the sql database – Deepak John Jan 12 '16 at 11:43

0 Answers0