abhinavrathore019
abhinavrathore019

Reputation: 6635

to insert float values entered in EditText into databse of android thru sqlite helper

I am new to java and new to Android also... i tried to search the same problem but i am sorry if itz already there as i was not able to understand that if any....

problem is related to typecasting.. what i think so ...

public class Ingredients_Add extends Activity {

    EditText IngredientName=null;
    EditText IngredientQuantity=null;
    EditText IngredientUnit=null;
    EditText IngredientCost=null;
    MenuManagement_SQLHelper helper=null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);       
            setContentView(R.layout.ingredients_add);

        helper=new MenuManagement_SQLHelper(this);
        IngredientName=(EditText)findViewById(R.id.lnrAddIngredients_edtxtName);
        IngredientQuantity=(EditText)findViewById(R.id.lnrAddIngredients_edtxtQuantity);
        IngredientUnit=(EditText)findViewById(R.id.lnrAddIngredients_edtxtUnit);
        IngredientCost=(EditText)findViewById(R.id.lnrAddIngredients_edtxtCost);

        float IngredientQuantityfloat=Float.valueOf(IngredientQuantity.getText().toString());
        float IngredientCostfloat=Float.valueOf(IngredientCost.getText().toString()); 

        Button SaveIngredients=(Button)findViewById(R.id.lnrAddIngredients_btnSave);
        SaveIngredients.setOnClickListener(onSave);
    }

I have problem in

IngredientQuantity=(EditText)findViewById(R.id.lnrAddIngredients_edtxtQuantity);
IngredientCost=(EditText)findViewById(R.id.lnrAddIngredients_edtxtCost);

These two edit boxes return Float values entered by the user...

Following is my onsave code for button click which passes the values to the SQLHelper class here i named that "MenuManagement_SQLHelper"

private View.OnClickListener onSave=new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            helper.insertIngredients(IngredientName.getText().toString(), IngredientQuantityfloat, IngredientUnit.getText().toString(), IngredientCostfloat);
        }
    };

the following code is for sqlhelper class :-

public class MenuManagement_SQLHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME="menu_management.db";
    private static final int SCHEMA_VERSION=1;

    public MenuManagement_SQLHelper(Context context) {
        super(context, DATABASE_NAME,null,SCHEMA_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE tblIngredients (_id INTEGER PRIMARY KEY AUTOINCREMENT, Ingredient_name TEXT,Ingredient_quantity FLOAT,Ingredient_unit TEXT,Ingredient_cost FLOAT);");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

    public void insertIngredients(String IngredientName,Float IngredientQuantity,String IngredientUnit,Float Ingredientcost){
        ContentValues cv= new ContentValues();
        cv.put("IngredientName", IngredientName);
        cv.put("IngredientQuantity", IngredientQuantity);
        cv.put("IngredientUnit", IngredientUnit);
        cv.put("Ingredientcost", Ingredientcost);
        getWritableDatabase().insert("tblIngredients","IngredientName", cv);
    }

problem I am facing :- IngredientQuantityfloat cannot be resolved to a variable IngredientQuantityfloat cannot be resolved to a variable

the problem is coming for float variables... earlier it was function mismatch as i was passing strings and there in sqlhelper it was declared as float -> "that u have to convert it into float"

after parsing now it is giving that

cannot be resolved to variable ...

sdk information(although not required)

 android:versionCode="1"
android:versionName="1.0" 
android:minSdkVersion="8"

thnx in advance

Upvotes: 0

Views: 1174

Answers (1)

MH.
MH.

Reputation: 45493

It looks like a scope problem to me. You defined IngredientQuantityfloat in the onCreate method of your activity, but you're (probably) trying to reference it in your OnClickListener (onSave?).

public void onCreate(Bundle savedInstanceState) {
    ...
    float IngredientQuantityfloat=Float.valueOf(IngredientQuantity.getText().toString());
    ...
}

public void onClick(View v) {
    ...
    helper.insertIngredients(IngredientName.getText().toString(), IngredientQuantityfloat, IngredientUnit.getText().toString(), IngredientCostfloat);
    ...
}

In other words: IngredientQuantityfloat is locally declared and only exists within the onCreate method. If you want to access it from another part of your activity, you need to make it a class variable/member:

public class Ingredients_Add extends Activity {

    float IngredientQuantityfloat;
    EditText IngredientName=null;
    ...

    public void onCreate(Bundle savedInstanceState) {
       ...
       IngredientQuantityfloat=Float.valueOf(IngredientQuantity.getText().toString());
       ...
    }
}

Upvotes: 1

Related Questions