Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7429535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:59:05+00:00 2026-05-29T08:59:05+00:00

I have 2 text fields on the screen and a button. When the button

  • 0

I have 2 text fields on the screen and a button. When the button is pressed I want the information in the name text field to be passed to a variable, then run my addAccount() method in my DatabaseHelper.class, passing the variable into this method, this should store the variable information in an SQLite database.

Here is my Activity with the text field and button.

    package mr.mwod.moneyorganiser;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;

public class AddAccountActivity extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);       
        setContentView(R.layout.addaccountlayout);

        Button addAccountVairiable = (Button) findViewById(R.id.addAccountButton);
        addAccountVairiable.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                EditText accountNameData = (EditText)findViewById(R.id.accountNameTextField);
                String accountNameVairiable = accountNameData.getText().toString();
                //Need the code here to run addAccount form DatabaseHelper.class
                //accountName vairiable needs to be passed into the method as the information to store.
            }
        });
    }
}

I have commented where I think the code needs to go and what it needs to do.

Here is my DatabaseHelper.class encase their are errors is in there.

package mr.mwod.moneyorganiser;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "money_organiser";
    private static final String TABLE_ACCOUNTS = "accounts";
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "account_name";
    private static final String KEY_ACCOUNT_BALANCE = "account_balance";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_ACCOUNTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_ACCOUNT_BALANCE + " INTEGER" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ACCOUNTS);
        // Create tables again
        onCreate(db);
    }

    public void addAccount(String name) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, name);

        db.insert(TABLE_ACCOUNTS, null, values);
        db.close();
    }
}

Any help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:59:06+00:00Added an answer on May 29, 2026 at 8:59 am

    Is there any trick in your question? Isn’t just calling new DatabaseHelper(this).addAccount(accountNameVairiable); working? Of course it will make sense to reuse the variable, rather than allocating it every time, but I am almost sure this is not your problem.

    By the way I would recommend you to change the name of the primary key column of the table. Use _id, some android helper methods will be easier to use in this way.

    EDIT: I just noticed the call happens in anonymous class. This as noted by you will trigger a compilation error, because the listener is not a Context. The activity is. Luckily this can be very easily solved via placing what class exactly to use for the ocnstruction (using the parent Activity, which is a context).

    new DatabaseHelper(AddAccountActivity.this).addAccount(accountNameVairiable);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Creating a JApplet I have 2 Text Fields, a button and a Text Area.
I have one form with several text fields and one button. When i enter
I have an application where i have a screen with some edit text fields
I have a button on my home screen that will add an edit text
I have two text fields in my view. I did it using IB. My
I have n number of text fields named in the form Question...... How can
I have a form with two text_fields: <input type=text id=post_name name=post[name] /> <input type=text
I have an order form with about 30 text fields that contain numerical values.
I have a database full of text fields that look like this: (paragraph of
I have a page with .Net grid view with about 12 text fields per

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.