I am making an application to Store textfield values to database.After entering values when I press the button to submit it, it just hanged and give no response.
my onCreate code is given below:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final DBAdapter db = new DBAdapter(this);
//---add 2 titles---
db.open();
final long d_id ;
final EditText input1 = (EditText) findViewById(R.id.isbn);
final String isbn = input1.getText().toString();
final EditText input2 = (EditText) findViewById(R.id.titleText);
final String tText = input2.getText().toString();
final EditText input3 = (EditText) findViewById(R.id.publisherText);
final String pText = input3.getText().toString();
this.btn = (Button)this.findViewById(R.id.button1);
this.btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
db.insertTitle(isbn, tText, pText);
}
});
db.close();
}
}
isbn,titleText and publisherText are my id for Textfield.
insertTitle is function to insert the values.
Whats wrong with my code?
thanks.
You are reading the values of your text fields as the app is being created, not when the button is pushed. You will always enter the initial values of your EditTexts with your current code. Try this:
You will have to move where you close your database, otherwise you will get an error for attempting to access a closed database. Try putting it in onPause() or onDestroy().
If this isn’t the solution please post your logcat errors so we don’t have to guess what is happening.