I am trying to get the primary key “_id” from the selected group using code
groupPositionID is a global variable initialized like this
and IngredientListGroup_cursor is a group cursor which fetches the group data for expanable list view.
String groupPositionID=null;
Cursor IngredientListGroup_cursor;
in the oncreate code:
public void onCreate(Bundle savedInstanceState) {
IngredientListGroup_cursor=helper.GetIngredientsList();
}
ExpandableIngredietnsList.setOnGroupClickListener(new OnGroupClickListener(){
@Override
public boolean onGroupClick(
ExpandableListView paramExpandableListView,
View paramView, int paramInt, long paramLong) {
// TODO Auto-generated method stub
groupPositionID=IngredientListGroup_cursor.getString(0);
Toast toast = Toast.makeText(getBaseContext(),groupPositionID ,Toast.LENGTH_LONG);
toast.show();
return false;
}
});
this is my SQLhelper function for deleting the selected group from database.
public Cursor GetIngredientsList(){
return(getReadableDatabase().rawQuery("SELECT _id,Ingredient_name FROM tblIngredients",null));
}
public Cursor DeleteIngredientsList_Item(String index){
String[] args={index};
return(getReadableDatabase().rawQuery("DELETE FROM tblIngredients WHERE _id=?",args));
}
The problem I am facing is tha although i am getting the id for the selected group from cursor (i m able to see that as toast) but then, why i am not able to delete that from context menu.
on selecting delete from context menu the following code should execute properly
public boolean onOptionsItemSelected(MenuItem IngredientItem){
if(IngredientItem.getItemId()==R.id.addIngredient){
Intent i= new Intent(Ingredients_List.this,Ingredients_Add.class);
startActivity(i);
return(true);
}
else if (IngredientItem.getItemId()==R.id.deleteIngredient) {
if(groupPositionID!=null){
helper.DeleteIngredientsList_Item(groupPositionID);
return(true);
}
else{
Toast.makeText(getBaseContext(), "Select The Ingredient You want to delete", Toast.LENGTH_LONG).show();
}
}
return (super.onOptionsItemSelected(IngredientItem));
}
I am getting the selected primary key id value from “groupPositionID” variable here.. accurate value is transferred to database raw query also… still not getting what i am expecting
Plz help ASAP I am new to both android and java
sdk information(although not required)
android:versionCode="1"
android:versionName="1.0"
android:minSdkVersion="8"
Thanks in Advance
i modified this delete code in mysql helper
my function was earlier returning cursor thats why it was not working … meanz in actual query was not getting executed…
rest was ok.. 🙂