1

Below is my code ; I cannot insert data on button click as my program stop running when I click on submit button my code is as follows:

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class insertData extends Activity {
    /** Called when the activity is first created. */
    SQLiteDatabase db;
    ContentValues c;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); setContentView(R.layout.main);
        final EditText name=(EditText) findViewById(R.id.edtname);
        final EditText address=(EditText) findViewById(R.id.edtaddress);
        final EditText policyNo=(EditText) findViewById(R.id.edtpolicyno);
        Button submit=(Button) findViewById(R.id.submit);
        final TextView text = new TextView(getApplicationContext());
        text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        setContentView(R.layout.main);
        c = new ContentValues();     
        db = openOrCreateDatabase(
            "TestingData.db"
            , SQLiteDatabase.CREATE_IF_NECESSARY
            , null
            );
        db.setVersion(1);
        db.setLocale(Locale.getDefault());

       // String qry="CREATE TABLE test1(name text,address text,policy text);";
       // db.execSQL(qry);

        submit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(getApplicationContext(),"heloo",Toast.LENGTH_SHORT).show();

                c.put("Name",name.getText().toString().trim());
                c.put("Address",address.getText().toString().trim());
                c.put("PolicyNo",policyNo.getText().toString().trim()); 
                db.insert("test1", null, c);


                Cursor retrive = db.query("test11",null ,null, null, null, null, null);
                retrive.moveToFirst();
                 for(int i=0;i<retrive.getCount();i++)
                 {
                     String nm = retrive.getString(1);
                     String add=retrive.getString(2);
                     String polno=retrive.getString(3);
                     text.setText(nm+" "+add+" "+polno);                     

                 }//for ends/......  
           //  retrive.close();

            }

        });
    }
}

I don't know what happens here; please help me.

4
  • 2
    How did you get a +1 vote for this very general question you have asked, without even a log cat output of your application? Commented Jun 25, 2012 at 18:37
  • Might be friend of OP upvoted :)... Commented Jun 25, 2012 at 18:49
  • Wesley,user1042031, don't know who have voted and no error in log cat displayed that is why not given logcat ,ohterwise i don't need to upload question Commented Jun 26, 2012 at 4:41
  • the fact that you have an execSQL statement in your Activity tells me you are doing something very wrong... Commented Jun 27, 2012 at 12:46

2 Answers 2

2

You haven't posted your logcat dump but the first thing I noticed was that you are creating a table named "test1" and inserting/querying data into "test11".

Sign up to request clarification or add additional context in comments.

1 Comment

just ignore it that was changes i made when doing R&D i'll correct it
0

my button click event is not assigned memory therefore declare button diffrerent as beloow so my click event detected and fired

public class insertData extends Activity {
    /** Called when the activity is first created. */
    SQLiteDatabase db;
    ContentValues c;
    Button submit;      <-------
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); sss
        final EditText name=(EditText) findViewById(R.id.edtname);
        final EditText address=(EditText) findViewById(R.id.edtaddress);
        final EditText policyNo=(EditText) findViewById(R.id.edtpolicyno);
        submit=(Button) findViewById(R.id.submit);

i dont know why but this solve my problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.