如何从Android列表中删除元素?

时间:2011-10-08 22:40:28

标签: android list

任何人都可以解释当点击同一个孩子时(childMap调用),如何从onChildClick下的可扩展列表中删除子项目?我尝试了一些似乎无法使其发挥作用的东西。

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;
}}

2 个答案:

答案 0 :(得分:0)

SimpleExpandableListAdapter的文档说:“一个简单的适配器,用于将静态数据映射到XML文件中定义的组视图和子视图。” See here。此适配器仅用于静态数据。如果要删除项目,则应创建一个包含更新内容的新SimpleExpandableListAdapter。

答案 1 :(得分:0)

dw lol我通过创建一个包含更新内容的新数组来解决这个问题:

public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

    Common variableContext = ((Common)getApplicationContext());
    /* show a toast of the deleted value */
    Toast.makeText(this, "Deleting £"+variableContext.infoArray[childPosition]+" From list", Toast.LENGTH_LONG).show();

    /* delete and minus the value from the expense variable */
    int expenseValueAtChild = Integer.valueOf(variableContext.infoArray[childPosition]);
    variableContext.addToExpense(expenseValueAtChild);

    /* take one away from 'arrayLength' */
    variableContext.minusExpenseCounter();

    /* create a new array with updated values */
    int k = 0;
    for (int i = 0; i < arrayLength; i++) {
        if (i != childPosition) {
            variableContext.updatedInfoArray[k++] = variableContext.infoArray[i];
        }
    }
    /* replace the old array with the updated one and reload the screen */
    variableContext.infoArray = variableContext.updatedInfoArray;

    Intent reloadES = new Intent(Expense1.this, Expense1.class);
    startActivity(reloadES);
    finish();

    /* onChildClick must return a boolean */
    return true;
}