• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

to add data to database using edittext and button in android

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}

 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Well THAT's new! Comfort me, reliable tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic