0

I have got in trouble about how to modify the value in an ArrayList< HashMap< String, String>>.

For example, I have a ArrayList

1 one

2 two

3 three

and now I would like to edit 1 one to 0 zero then the list will as

0 zero

2 two

3 three

how do I do that?

sorry, i forgot to post my code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getLayoutInflater().inflate(R.layout.budgetcounting, contentFrame);


    setTitle(drawer_menu[3]);
    listView = (ListView)findViewById(R.id.bc_listItems);

    list = new ArrayList<HashMap<String, String>>();

    balance = (TextView)findViewById(R.id.bc_balance);
    budget = (TextView)findViewById(R.id.bc_budget);
    balance.setText("Balance: ");
    budget.setText("Budget: ");

    addBtn = (Button)findViewById(R.id.bc_btnadd);
    budgetBtn = (Button)findViewById(R.id.bc_btnbudget);

    addBtn.setOnClickListener(this);
    budgetBtn.setOnClickListener(this);

    HashMap<String, String> temp = new HashMap<String, String>();
    temp.put(FIRST_COLUMN, "1");
    temp.put(SECOND_COLUMN, "one");

    HashMap<String, String> temp1 = new HashMap<String, String>();
    temp1.put(FIRST_COLUMN, "2");
    temp1.put(SECOND_COLUMN, "two");

    HashMap<String, String> temp2 = new HashMap<String, String>();
    temp2.put(FIRST_COLUMN, "3");
    temp2.put(SECOND_COLUMN, "three");

    list.add(temp);
    list.add(temp1);
    list.add(temp2);

    adapter = new BC_ListViewAdapter(this, list);
    listView.setAdapter(adapter);

    registerForContextMenu(listView);
}

Here's the code for the delete part:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.bc_contextmenu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    switch (item.getItemId()) {
        case R.id.bc_contextmenu_delete:
            list.remove(info.position);
            adapter.notifyDataSetChanged();
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

Adapter:

public class BC_ListViewAdapter extends BaseAdapter {

public ArrayList<HashMap<String, String>> list;
Activity activity;

TextView txtFirst;
TextView txtSecond;

public BC_ListViewAdapter(Activity activity, ArrayList<HashMap<String, String>> list) {
    super();
    this.activity = activity;
    this.list = list;
}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = activity.getLayoutInflater();

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.bc_column, null);

        txtFirst = (TextView) convertView.findViewById(R.id.bc_itemname);
        txtSecond = (TextView) convertView.findViewById(R.id.bc_itemvalue);
    }
        HashMap<String, String> map = list.get(position);
        txtFirst.setText(map.get(FIRST_COLUMN));
        txtSecond.setText(map.get(SECOND_COLUMN));

        return convertView;
}
}
3
  • where is your code? what you have tried? Commented Jan 23, 2016 at 13:10
  • 1
    Is that a list with three maps (one entry each) or a list with one map (three entries)? Commented Jan 23, 2016 at 13:11
  • sorry, i forgot to post my code. Here I am trying to edit those value. I wish to do a counter in the future. That is I have 10 dollar. I add a sweet which cost 8 dollar into the list. That the application would shows I have 2 dollars left. But I need to edit the value in case I type in the wrong value. Thanks a lot! Commented Jan 23, 2016 at 13:21

1 Answer 1

1

Is that what you need?

list.get(0).put(FIRST_COLUMN, "0");
list.get(0).put(SECOND_COLUMN, "zero");

EDIT:

Change this in your adapter,

@Override
public long getItemId(int position) {
    return position;  //instead of zero
}

And use list.remove(info.id) to remove item from list

Sign up to request clarification or add additional context in comments.

7 Comments

Is it really need to remove and add? I can't directly edit these values? anyway, Thank You! But I have a question. Once I remove the 1 one, position[0] in the list would become empty. I wonder that if 2 two (position[1]) would change position[0]?
Thank you! Last question. I would use context menu to do the remove and edit part. Once I use list.remove(info.position); it will always remove the last item that is 3 three from the list no matter which item I choose to delete.
Use list.remove(info.id); instead of list.remove(info.position)
It does't work. It has't change anythings. I wonder if it related to the HashMap in the ArrayList. I also post the adapter. Would you mind to have a look. It may has something wrong to cause my problem.
it does't works anyway. I have tried to use list.remove(1) instead of list.remove(info.position) in order to remove 1 one. But it still remove the last item.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.