android hashmap get方法,关于android:在特定位置更新hashMap值

我尝试了很多方法来更新特定位置的hashMap值,但我无法做到。 我有一个自定义数组列表,其中我存储了特定位置数组列表的哈希映射数组列表。 在我的列表视图适配器中,我根据hashmap数组列表的数量创建动态线性布局,在该线性布局中我也有编辑文本。

现在点击每个线性布局项目的编辑文本。 我必须更新该特定位置和特定值的哈希映射值。

我的适配器代码是这样的: -

for (j = 0; j < arry_tickt_info.get(position).getArray_ticket_data().size(); j++) {

// Create LinearLayout

LinearLayout ll = new LinearLayout(context);

ll.setOrientation(LinearLayout.HORIZONTAL);

LinearLayout.LayoutParams qntityParams = new LinearLayout.LayoutParams(0,60, 1f);

// Create Quantitiy EditText

final EditText edQntity = new EditText(context);

final ArrayList> hashMaps = arry_tickt_info.get(j).getArray_ticket_data();

edQntity.setText(arry_tickt_info.get(position).getArray_ticket_data().get(j).get("quantity"));

edQntity.setOnFocusChangeListener(new View.OnFocusChangeListener() {

@Override

public void onFocusChange(View v, boolean hasFocus) {

if (!hasFocus) {

final int position = v.getId();

for (Map.Entry e1 : hashMaps.get(Integer.parseInt(txtHiddenId.getText().toString())).entrySet()) {

String s = e1.getValue();

String s1 = e1.getKey();

if (e1.getKey().equalsIgnoreCase("quantity")) {

String roundedOff = edQntity.getText().toString();

e1.setValue(String.valueOf(roundedOff));

Log.e("updatedValue::::","updatedValue::::" + roundedOff);

}

}

}

}

});

edQntity.setGravity(Gravity.CENTER_HORIZONTAL);

edQntity.setEms(2);

edQntity.setHeight(60);

edQntity.setBackgroundResource(R.drawable.edittext_grey_outline);

edQntity.setTextColor(Color.parseColor("#555555"));

edQntity.setLayoutParams(qntityParams);

ll.addView(edQntity);

}

请发一些代码

我已经用代码更新了问题。

考虑到java hashmap中的键,如何更新值可能重复?

实际上我只需更新我已更改值的编辑文本的值。 在我的代码中,它更新列表项的所有值,其中position与hash map index匹配。 据我所知,我们只能通过迭代更新hashmap值。 我陷入了这段代码。

不确定你要做什么,但是要更新Hashmap值,你可以使用相同的put函数,使用它来设置之前的值。当你使用put时,它会创建带有该键的行,如果还没有,如果key存在,那么它将更新该键的值。

第一次做的时候: -

map.put("key","value1");

第二次想要更新: -

map.put ("key","value2"); //remember you need to have exact same key you used previously while inserting an entry.

对于arraylist你有设置(索引,值);即

ArrayList alStr = new ArrayList();

alStr.add ("abc");

用于更新"abc"的查找索引然后

alStr.add (0,"abc"); //used 0 as currently we have only one item.

用这个

HashMap map = new HashMap();

map.put("Your_specific_key","Value");

map.put("Your_specific_key","Value");

map.put("Your_specific_key","Value");

your.add(Position_num, map);

对于

/**

* Inserts the specified element at the specified position in this

* list. Shifts the element currently at that position (if any) and

* any subsequent elements to the right (adds one to their indices).

*

* @param index index at which the specified element is to be inserted

* @param element element to be inserted

* @throws IndexOutOfBoundsException {@inheritDoc}

*/

public void add(int index, E element) {

if (index > size || index < 0)

throw new IndexOutOfBoundsException(outOfBoundsMsg(index));

ensureCapacityInternal(size + 1);  // Increments modCount!!

System.arraycopy(elementData, index, elementData, index + 1,

size - index);

elementData[index] = element;

size++;

}

我可以告诉你一种降低复杂性的快捷方法

只需在视图标记edQntity.setTag(KeyForThisView)中设置密钥即可

现在,对于此视图的事件侦听器,您只需使用edQntity.getTag()来检索键。

因为你有密钥只是更新hashMap中的值。为什么这样繁重的操作再次遍历整个地图。

您可以使用密钥替换(键,值)方法更新HashMap值。

String s1 = e1.getKey();

hashMaps.replace(s1, roundedOff); //HashMap  s1 key value replaced by roundedOff value.

我也尝试了这个解决方案,但它在该特定位置的hashmap数组列表中添加了一个新值。

检查我的编辑答案

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值