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