• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

to add data to database using edittext and button in android

 
avinashguru prasad
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code for inserting data to database but it fails to add data to database.
Breakpoint comes at the statement-"setContentView(R.layout.activity_main);"




package com.avi.datainsertclick;

import java.util.ArrayList;



import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Toast;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;


public class MainActivity extends Activity {
EditText utext,ptext;
Button lbtn;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

final DBAdapter db=new DBAdapter(this);

db.open();
/* long id=db.insertContact("wei-meng lee","ak10698@gmail.com");
db.close();*/
Button btn1 = (Button) findViewById(R.id.btn1);
utext = (EditText) findViewById(R.id.et1);
ptext = (EditText) findViewById(R.id.et2);
btn1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
String ut = utext.getText().toString();
String pt = ptext.getText().toString();


try {

db.insertContact(ut, pt);

} catch (Exception e) {




Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
}

}
});

/* Cursor cursor = db.getAllContacts();
for(int i = 0; i < cursor.getCount(); i++) {
cursor.moveToPosition(i);
int rid = cursor.getInt(cursor.getColumnIndexOrThrow(DBAdapter.KEY_ROWID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(DBAdapter.KEY_NAME));
String email = cursor.getString(cursor.getColumnIndexOrThrow(DBAdapter.KEY_EMAIL));
Toast.makeText(getApplicationContext(), rid + "-" + name + "-" + email, Toast.LENGTH_LONG).show();

}*/
cursor.close();
db.close();
}


}


inside class 'DBAdapter' this is the mathod



public long insertContact(String name,String email) {

ContentValues initialValues=new ContentValues();
initialValues.put(KEY_NAME,name);
initialValues.put(KEY_EMAIL,email);
return db.insert(DATABASE_TABLE, null, initialValues);
}

 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you edit your mesgsage and UseCodeCodeTags please?

What does LogCat tell you? Any errors?
 
avinashguru prasad
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:Can you edit your mesgsage and UseCodeCodeTags please?

What does LogCat tell you? Any errors?




ActivityManager: Warning: Activity not started, its current task has been brought to the front.
and also suspended breakpoint at statement="setContentView(R.layout.activity_main);"
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
avinashguru prasad wrote:ActivityManager: Warning: Activity not started, its current task has been brought to the front.
and also suspended breakpoint at statement="setContentView(R.layout.activity_main);"


Is that LogCat or the console?

It looks like you application is at a breakpoint - no error. If you are in Eclipse, there should be a debug perspective with an icon you can use to step through the breakpoint. Or you can press F8 while Eclipse is selected to step through.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic