0

I am new to Android,forgive if the question seems silly to you. I am trying to develop android application "Home Accounts Manager". For this i have used SQLite database to insert data in the database from my second activity. On my second activity what i have implemented is that after click of the button on second activity,the text present on all the edittext field will be inserted into the database. But nothing is happening after the click.

Second Activity::

package com.example.accountmanager;

import com.example.accountmanager.*;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;

public class ExpenseEngine extends ActionBarActivity {

EditText fixedExp,person1,person2,person3;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expense_engine);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
                .add(R.id.ExpenseEngine, new PlaceholderFragment())
                .commit();
    }

   fixedExp=(EditText) findViewById(R.id.editText1);
   person1=(EditText) findViewById(R.id.editText2);
   person2=(EditText) findViewById(R.id.editText3);
   person3=(EditText) findViewById(R.id.editText4);

Button but = (Button)findViewById(R.id.button1);
but.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        newExpense(v);
    }
});

}
public void newExpense(View view){

    AccountDBHelper accDBHelper=new AccountDBHelper(this);
    int fixedExpense=Integer.parseInt(fixedExp.getText().toString());
    int person_1=Integer.parseInt(person1.getText().toString());
    int person_2=Integer.parseInt(person2.getText().toString());
    int person_3=Integer.parseInt(person3.getText().toString());
    AccountModel accModel=new AccountModel(fixedExpense,person_1,person_2,person_3);
    accDBHelper.insertData(accModel);
    fixedExp.setText("");
    person1.setText("");
    person2.setText("");
    person3.setText("");

} 

public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
     getMenuInflater().inflate(R.menu.main_activity_actions, menu);
     return super.onCreateOptionsMenu(menu);
}


public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.expense_engine, container, false);
        return rootView;
    }
}

}

` `<?xml version="1.0" encoding="UTF-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#66ccff"
android:orientation="vertical"
android:id="@+id/ExpenseEngine"
 >

<TextView
    android:id="@+id/textView4"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal"
    android:layout_marginLeft="17dp"
    android:text="@string/tot_exp"
    android:textColor="#ffff00"
    android:textSize="10pt"
    android:textStyle="bold" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:text="@string/p2_exp"
    android:textColor="#ffff00"
    android:textSize="10pt"
    android:textStyle="bold" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="67dp"
    android:text="@string/p3_exp"
    android:textColor="#ffff00"
    android:textSize="10pt"
    android:textStyle="bold" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView3"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/textView3"
    android:ems="10"
    android:hint="@string/hint"
    android:textColorHint="#ffffff"
    android:textSize="12sp" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_alignTop="@+id/textView2"
    android:ems="10"
    android:hint="@string/hint"
    android:textColorHint="#ffffff"
    android:textSize="12sp" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView1"
    android:layout_alignLeft="@+id/editText2"
    android:ems="10"
    android:hint="@string/hint"
    android:textColorHint="#ffffff"
    android:textSize="12sp" />

<EditText
    android:id="@+id/editText4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView4"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="18dp"
    android:ems="10" 
    android:hint="@string/hint_fixed"
    android:textColorHint="#ffffff"
    android:textSize="12sp"/>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="35dp"
    android:text="Checkout" 
    />

<TextView
    android:id="@+id/textView3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText4"
    android:layout_marginTop="42dp"
    android:text="@string/p1_exp"
    android:textColor="#ffff00"
    android:textSize="10pt"
    android:textStyle="bold" />

`

package com.example.accountmanager;

import com.example.accountmanager.DBContract.DBEntry;
import com.example.accountmanager.AccountModel;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class AccountDBHelper extends  SQLiteOpenHelper{


public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "Account.db";
SQLiteDatabase db;
     ContentValues values;
     long newRowId;
     String id,p1,p2,p3;


    public AccountDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DBEntry.SQL_CREATE_ENTRIES);

    }
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // This database is only a cache for online data, so its upgrade policy is
        // to simply to discard the data and start over
        db.execSQL(DBEntry.SQL_DELETE_ENTRIES);
        onCreate(db);
    }
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }

    public void insertData(AccountModel accModel){

        ContentValues values=new ContentValues();
        values.put(DBEntry.COLUMN_NAME_ID,accModel.getFixedExp());
        values.put(DBEntry.COLUMN_NAME_PERSON1,accModel.getPerson1());
        values.put(DBEntry.COLUMN_NAME_PERSON2,accModel.getPerson2());
        values.put(DBEntry.COLUMN_NAME_PERSON3,accModel.getPerson3());
        SQLiteDatabase db=this.getWritableDatabase();
        db.insert(DATABASE_NAME, DBEntry.COLUMN_NAME_NULLABLE, values);
        db.close();
    }


}





package com.example.accountmanager;

public class AccountModel {

int fixedExp,person1,person2,person3;
public AccountModel(int fixedExp,int person1,int person2,int person3){

    this.fixedExp=fixedExp;
    this.person1=person1;
    this.person2=person2;
    this.person3=person3;
}

public void setFixedExp(int fixed){
    this.fixedExp=fixed;

}

public int getFixedExp(){
    return fixedExp;
}

public void setPerson1(int person1){
    this.person1=person1;
}

public int getPerson1(){
    return person1;
}
public void setPerson2(int person2){
    this.person1=person2;
}

public int getPerson2(){
    return person2;
}
public void setPerson3(int person3){
    this.person1=person3;
}

public int getPerson3(){
    return person3;
}

}

package com.example.accountmanager;

import android.provider.BaseColumns;

public final class DBContract {

public DBContract(){}

public static abstract class DBEntry implements BaseColumns{
    public static final String TABLE_NAME="acc_entry";
    public static final String COLUMN_NAME_ID="acc_entry_id";
    public static final String COLUMN_NAME_PERSON1="p1_entry";
    public static final String COLUMN_NAME_PERSON2="p2_entry";
    public static final String COLUMN_NAME_PERSON3="p3_entry";
    public static final String COLUMN_NAME_NULLABLE="null";
    public static final String TEXT_TYPE = " TEXT";
    public static final String COMMA_SEP = ",";
    public static final String SQL_CREATE_ENTRIES =
        "CREATE TABLE " + DBEntry.TABLE_NAME + " (" +
        DBEntry.COLUMN_NAME_ID + " INTEGER PRIMARY KEY," +
        DBEntry.COLUMN_NAME_PERSON1+ TEXT_TYPE + COMMA_SEP +
        DBEntry.COLUMN_NAME_PERSON2 + TEXT_TYPE + COMMA_SEP +DBEntry.COLUMN_NAME_PERSON3+
        TEXT_TYPE+")";


     public static final String SQL_DELETE_ENTRIES =
        "DROP TABLE IF EXISTS " + DBEntry.TABLE_NAME;



}

}

I have installed the sqlite plugin to view my tables also,but nothing is present in the file explorer tab of DDMS and there is blurred icon of the SQLite database which seems to be disabled.

9
  • there might be some information in logcat? Commented Jun 11, 2014 at 4:50
  • u log the string SQL_CREATE_ENTRIES and tell me Commented Jun 11, 2014 at 4:58
  • also change the database name "Account.db" to "Account" Commented Jun 11, 2014 at 5:00
  • "But nothing is happening after the click".When you use fragment I think you should findView in fragment, Is it work? If you move all your View (EditText, Button) to method onCreateView() in inner class PlaceholderFragment Commented Jun 11, 2014 at 5:06
  • @Bipin,its not working :( Commented Jun 11, 2014 at 13:17

1 Answer 1

1
 db.insert(DATABASE_NAME, null, values); 

instead of

db.insert(DATABASE_NAME, DBEntry.COLUMN_NAME_NULLABLE, values);

could be the solution. Try it!

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

Comments

Your Answer

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