1

Hi I am new to android and I want to know how I can add buttons to my app programmatically . Actually the scenario is like this: In my app, I am having several default categories(given as buttons) to save data. I must provide an option add categories. When I click on the add categories button I must get an option to specify the category name and the sub category name(which i can do). But the thing is that I must get a new button with the entered name and it should not get deleted when i leave the application. Any help is highly appreciated.

Balaji Khadake
  • 3,449
  • 4
  • 24
  • 38
krf
  • 29
  • 1
  • permanent buttons really don't make sense.When you will leave the app,how and where would you need to see the buttons?? – Hiral Vadodaria Oct 17 '11 at 09:52
  • i dont want those buttons to get deleted. I want those buttons to stay permanent so that when i enter the application again, the added category(button) must be there. – krf Oct 17 '11 at 09:55
  • Then you must save them somewhere and create your view from stored categories data. Don't you save the category permenantly..? – Hiral Vadodaria Oct 17 '11 at 09:59
  • ya but that's what i dont know how to do. – krf Oct 17 '11 at 10:03

1 Answers1

0

To create database: refer How do I create a database in android?

Then you need to use DatabaseHelper class to insert,update or delete data on your database. refer http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html

Now you must be able to manage your data using DatabaseHelper class's object in your activity.

1.DatabaseCreator.java

public class DatabaseCreator extends  SQLiteOpenHelper{

    private static final String DB_NAME="database_name";
    private static final int DB_VER=1;

    private static final String TABLE_NAME="create table table_name(_id integer primary key autoincrement,field1 text not null,field2 text not null)";

    public DatabaseCreator(Context context)
    {
        super(context,DB_NAME, null, DB_VER);
    }

    @Override
    public void onCreate(SQLiteDatabase database)
    {
        database.execSQL(TABLE_NAME);       
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int arg1, int arg2) {

        database.execSQL("DROP TABLE IF EXISTS table_name");

        onCreate(database);
    }
}

2.DatabaseHelper.java

public class DatabaseHelper {
public static final String TABLENAME_DB="table_name";

  public static final String KEY_ID="_id";
  public static final String KEY_FIELD1="field1";
  public static final String KEY_FIELD2="field2";

  Context context;
  SQLiteDatabase sqdb;
  DatabaseCreator dbcreator;

  public DatabaseHelper(Context context)
      {
    this.context=context;
  }

  public DatabaseHelper open() throws SQLException
  {
    dbcreator=new DatabaseCreator(context);
    sqdb=dbcreator.getWritableDatabase();
    return this;
  }

  public void close()
  {
    dbcreator.close();
  }

  public long addItem(String field1,String field2)
  {
    ContentValues values=new ContentValues();
    values.put(KEY_FIELD1,field1);
    values.put(KEY_FIELD2,field2);
    return sqdb.insert(TABLENAME_DB,null,values);
  }

  public long deletItem(long _id)
  {
    return sqdb.delete(TABLENAME_DB, "_id="+_id,null);
  } 

}

Use this in your activity like:

DatabaseHelper db_helper=new DatabaseHelper(getApplicationContext());
db_helper.open();

db_helper.addItem("field1_value","field2_value");

db_helper.close();
Community
  • 1
  • 1
Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56