This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Android and the fly likes to add data to database using edittext and button in android Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "to add data to database using edittext and button in android" Watch "to add data to database using edittext and button in android" New topic
Author

to add data to database using edittext and button in android

avinashguru prasad
Greenhorn

Joined: Nov 23, 2012
Posts: 14
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

Joined: Jan 28, 2003
Posts: 3968
    
  17

Can you edit your mesgsage and UseCodeCodeTags please?

What does LogCat tell you? Any errors?


Steve
avinashguru prasad
Greenhorn

Joined: Nov 23, 2012
Posts: 14
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

Joined: Jan 28, 2003
Posts: 3968
    
  17

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: to add data to database using edittext and button in android
 
Similar Threads
ftp uploads but only the second time a picture is taken?
how to read table from mobile
SharedPreference not working
getting real path from an image uri after camera and uploading-getting null pointer exception
Cannot find the problem in the code