Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to delete elements from a list in Android?
    primarykey
    data
    text
    <p>Can anyone explain how to remove a child item from my expandable list under <code>childMap</code> when the same child is clicked (called with <code>onChildClick</code>)? I've tried a few things can't seem to make it work.</p> <pre><code>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() &lt; 1 &amp;&amp; expenseComment.length() &gt; 0 ) { new AlertDialog.Builder(Expense1.this).setTitle("Error") .setMessage("enter a value") .setPositiveButton("OK", null).show();} else if( expenseInfo.length() &gt;= 1 &amp;&amp; expenseComment.length() &lt; 1 ) { new AlertDialog.Builder(Expense1.this).setTitle("Error") .setMessage("enter a comment") .setPositiveButton("OK", null).show(); } else if( expenseInfo.length() &lt; 1 &amp;&amp; expenseComment.length() &lt; 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&lt;HashMap&lt;String, String&gt;&gt; createGroupList() { ArrayList&lt;HashMap&lt;String, String&gt;&gt; list = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); for( int i = 0 ; i &lt; GROUPS ; ++i ) { HashMap&lt;String, String&gt; groupMap = new HashMap&lt;String, String&gt;(); groupMap.put( GROUP_ID, "Group Item " + i ); list.add(groupMap); } return (List&lt;HashMap&lt;String, String&gt;&gt;)list; } /* create the HashMap for the children */ private List&lt;ArrayList&lt;HashMap&lt;String, String&gt;&gt;&gt; createChildList() { ArrayList&lt;ArrayList&lt;HashMap&lt;String, String&gt;&gt;&gt; result = new ArrayList&lt;ArrayList&lt;HashMap&lt;String, String&gt;&gt;&gt;(); ArrayList&lt;HashMap&lt;String, String&gt;&gt; subList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); Common variableContext = ((Common)getApplicationContext()); arrayLength = variableContext.returnExpenseCounter() + 1; for( int n = 0 ; n &lt; arrayLength ; n++ ) { HashMap&lt;String, String&gt; childMap = new HashMap&lt;String, String&gt;(); childMap.put( CHILD_ID, "Amount: "+"£"+variableContext.infoArray[n] ); subList.add(childMap); } for (int i = 0; i &lt; 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; }} </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload