如何修改ArrayList中的值<hashmap <string,string =“”>&gt;?

时间:2016-01-23 13:08:42

标签: android arraylist hashmap

我遇到了如何修改ArrayList中的值的问题&lt;的HashMap&LT;字符串,字符串&gt;&gt;。

例如, 我有一个ArrayList

1个

2两个

3三

现在我想将1编辑为0,然后将列表编辑为

0零

2两个

3三

我该怎么做?

抱歉,我忘了发布我的代码

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

这是删除部分的代码:

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

适配器:

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

1 个答案:

答案 0 :(得分:1)

这就是你需要的吗?

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

修改

在适配器中更改此内容,

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

并使用list.remove(info.id)从列表中删除项目