Can anyone explain how to remove a child item from my expandable list under childMap when the same child is clicked (called with onChildClick)? I’ve tried a few things can’t seem to make it work.
public class Expense1 extends ExpandableListActivity {
public static final String GROUP_ID = "Group";
public static final String CHILD_ID = "Child";
public static final int GROUPS = 1;
int arrayLength;
Button eButton;
EditText eEdit;
EditText nEdit;
public int expenseVar;
public String expenseComment;
public String expenseInfo;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expense1);
SimpleExpandableListAdapter expandableListAdapter =
new SimpleExpandableListAdapter(
this,
createGroupList(), // Creating group List.
R.layout.group_row, // Group item layout XML.
new String[] { GROUP_ID }, // the key of group item.
new int[] { R.id.group_text }, // ID of each group item.-Data under the key goes into this TextView.
createChildList(), // childData describes second-level entries.
R.layout.child_row, // Layout for sub-level entries(second level).
new String[] { CHILD_ID }, // Keys in childData maps to display.
new int[] { R.id.child_text } // Data under the keys above go into these TextViews.
);
setListAdapter(expandableListAdapter); // setting the adapter in the list.
eButton = (Button)findViewById(R.id.expense_update);
eEdit = (EditText)findViewById(R.id.expense_textField);
nEdit = (EditText)findViewById(R.id.expenseComment_textField);
eButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Common variableContext = ((Common)getApplicationContext());
expenseInfo = eEdit.getText().toString();
expenseComment = nEdit.getText().toString();
/* check REGULAR expense length and execute code */
if( expenseInfo.length() < 1 && expenseComment.length() > 0 ) {
new AlertDialog.Builder(Expense1.this).setTitle("Error")
.setMessage("enter a value")
.setPositiveButton("OK", null).show();}
else if( expenseInfo.length() >= 1 && expenseComment.length() < 1 ) {
new AlertDialog.Builder(Expense1.this).setTitle("Error")
.setMessage("enter a comment")
.setPositiveButton("OK", null).show();
}
else if( expenseInfo.length() < 1 && expenseComment.length() < 1 ) {
new AlertDialog.Builder(Expense1.this).setTitle("Error")
.setMessage("Please fill out fields")
.setPositiveButton("OK", null).show();
} else {
/* if both fields are correct then set the 'expenseVar' variable to the received integer:
* this stops the program crashing as it is executed after the fields are checked */
expenseVar = Integer.valueOf(eEdit.getText().toString());
/* set the values of the expense value and expense comment array by taking these from the fields
* when the button is clicked and assigning them to whatever the 'ReturnExpenseCounter' is on:
* the value returned depends on how many times the button has been clicked */
variableContext.setExpenseCounter();
String xxd = String.valueOf(expenseVar);
int dds = variableContext.returnExpenseCounter();
variableContext.infoArray[dds] = xxd;
variableContext.commentArray[dds] = expenseComment;
/* reload the screen to make the array list update */
Intent reloadScreen = new Intent(Expense1.this, Expense1.class);
startActivity(reloadScreen);
finish();
}
}
});
}
/* Creating the Hash-map for the row */
private List<HashMap<String, String>> createGroupList() {
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
for( int i = 0 ; i < GROUPS ; ++i ) {
HashMap<String, String> groupMap = new HashMap<String, String>();
groupMap.put( GROUP_ID, "Group Item " + i );
list.add(groupMap);
}
return (List<HashMap<String, String>>)list;
}
/* create the HashMap for the children */
private List<ArrayList<HashMap<String, String>>> createChildList() {
ArrayList<ArrayList<HashMap<String, String>>> result = new ArrayList<ArrayList<HashMap<String, String>>>();
ArrayList<HashMap<String, String>> subList = new ArrayList<HashMap<String, String>>();
Common variableContext = ((Common)getApplicationContext());
arrayLength = variableContext.returnExpenseCounter() + 1;
for( int n = 0 ; n < arrayLength ; n++ ) {
HashMap<String, String> childMap = new HashMap<String, String>();
childMap.put( CHILD_ID, "Amount: "+"£"+variableContext.infoArray[n] );
subList.add(childMap);
}
for (int i = 0; i < GROUPS; ++i) {
result.add(subList);
}
return result;
}
/* This function is called on each child click */
public boolean onChildClick( ExpandableListView parent, View v, int groupPosition,int childPosition,long id) {
switch(childPosition){
case 0:
//here for example i want to add something that removes an item from the child map
//in this case the item at the start (because its case 0)
Toast.makeText(this, "child 1", Toast.LENGTH_LONG).show();
Common variableContext = ((Common)getApplicationContext());
variableContext.minusExpenseCounter();
Intent bb = new Intent(Expense1.this, Expense1.class);
startActivity(bb);
finish();
break;
}
return true;
}}
The documentation of the SimpleExpandableListAdapter says: “An easy adapter to map static data to group and child views defined in an XML file.” See here. This adapter is only for static data. If you want to delete an item, you should create a new SimpleExpandableListAdapter with the updated content.