user5173426 user5173426 - 3 months ago 10
Android Question

Android: Passing values to another activity's function and then to Sqlite database

It's been a month since I've kicked off with android studio, at this time I'm stuck at a point where I have to pass a set of

<EditText/>
values from
MainActivity
to
UpdateActivity
and then send those values to the Sqlite database for updating a table.

What I have accomplished:

Successfully sending values from
MainActivity
's
<EditText/>
to
UpdateActivity
's
<EditText/>


How I have done that from
MainActivity
:

public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
EditText editName, editSurname, editMarks;
Button btnUpdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);

editName = (EditText) findViewById(R.id.UpdName);
editSurname = (EditText) findViewById(R.id.editTextSurname);
editMarks = (EditText) findViewById(R.id.UpdMarks);
btnUpdate = (Button)findViewById(R.id.btnUpdate);

btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String MainName = editName.getText().toString();
String MainSurname = editSurname.getText().toString();
String MainMarks = editMarks.getText().toString();

Intent UpdateAct = new Intent(MainActivity.this, UpdateActivity.class);
UpdateAct.putExtra("Name", MainName);
UpdateAct.putExtra("SurName",MainSurname );
UpdateAct.putExtra("Marks", MainMarks);
startActivity(UpdateAct);
}
});
}


Then inside the
UpdateActivity
:

public class UpdateActivity extends AppCompatActivity {
DatabaseHelper myDb;
Button btnUpd;
EditText editTextId, editUpdtName, editUpdSurname, editUpdMarks;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);

Intent getData = getIntent();
String Name = getData.getStringExtra("Name");
String SurName = getData.getStringExtra("SurName");
String Marks = getData.getStringExtra("Marks");

editTextId = (EditText)findViewById(R.id.editTextID);
editUpdtName = (EditText)findViewById(R.id.UpdName);
editUpdSurname = (EditText)findViewById(R.id.UpdSurname);
editUpdMarks = (EditText)findViewById(R.id.UpdMarks);
btnUpd = (Button)findViewById(R.Id.btnUpd);

editUpdtName.setText(Name);
editUpdSurname.setText(SurName);
editUpdMarks.setText(Marks);
}


What I'm stuck at is to send these values to a function named
updateData();

For that I have tried to use the values from
MainActivity
directly to
UpdateActivity
but failed, then I tried to cast those
<EditText/>
and use it inside the function but failed again.

Inside my
UpdateActivity
:

..//
editUpdtName.setText(Name);
editUpdSurname.setText(SurName);
editUpdMarks.setText(Marks);
//updateData(Name, SurName, Marks);
updateData();
}

//function definiton
// public void updateData(final String Name, final String SurName, final String Marks)
public void updateData()
{
btnUpd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// boolean isUpdated = myDb.updateData(editTextId.getText().toString(), Name,SurName, Marks);
boolean isUpdated = myDb.updateData(editTextId.getText().toString(), editUpdtName.getText().toString(),
editUpdSurname.getText().toString(), editUpdMarks.getText().toString());
if(isUpdated == true){
Toast.makeText(UpdateActivity.this, "Data Updated Successfully!", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(UpdateActivity.this, "Data NOT Updated!", Toast.LENGTH_SHORT).show();
}

}
});
}
}


The app crashes everytime the function
updateData();
is called, to ensure that is the real cause I have even commented and also tried putting it in a handler to run it after 3000ms, as expcted it crashed after the time period.

Solved it by initializing the db.

myDb = new DatabaseHelper(this);

Answer

You forgot to initialize myDb. Initialize it in your onCreate() method.

myDb = new DatabaseHelper(this);