0

I tried out lot my ways to update hashMap value on specific position but i am not able to do. I have a custom Array List in which i stored hash map array list for specific position array list. And in my list view adapter i am creating dynamic linear layout on the basis of count of hashmap array list, in that linear layout i have a edit Text also.

Now on click of edit text of each item of linear layout. i have to update the hash map value for that particular position and for particular value.

My Adapter code is like this:-

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<HashMap<String, String>> 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<String, String> 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);
}
4
  • 1
    post some code please Commented Jan 21, 2016 at 6:01
  • I have updated question with the code. Commented Jan 21, 2016 at 6:16
  • Possible duplicate of How to update a value, given a key in a java hashmap? Commented Jan 21, 2016 at 6:20
  • Actually i have to update only that value of edit text for which i have changed the value. In my code, it update all the value of list item where position matches with hash map index. In my knowledge we can update the hashmap value only with iteration. I am stuck in this code. Commented Jan 21, 2016 at 6:28

4 Answers 4

1

Not sure what you are trying to do, but to update Hashmap value you can use same put function using which you set the value previously. When you use put it will create the row with that key if not already there, if key is present then it will update the value for that key.

When doing for first time:-

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

second time when you want to update:-

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

For arraylist you have set(index, value); i.e.

ArrayList<String> alStr = new ArrayList<String>();

alStr.add ("abc");

for updating find index of "abc" and then

alStr.add (0, "abc"); //used 0 as currently we have only one item.
Sign up to request clarification or add additional context in comments.

Comments

0

you can update HashMap value by using key replace(key, value) method.

 String s1 = e1.getKey();  
 hashMaps.replace(s1, roundedOff); //HashMap  s1 key value replaced by roundedOff value.

1 Comment

I tried out this solution also but it added a new value in the hashmap array list for that particular position.
0

I can tell you one quick way to decrease the complexity

just set the key in the view tag edQntity.setTag(KeyForThisView) Now for this view's event listeners you can just use edQntity.getTag() to retrieve the key.

As you have the key just update the value in you hashMap. Why such heavy operation of traversing the whole map again.

Comments

0

use this

        HashMap<String, String> map = new HashMap<String, String>();
        map.put("Your_specific_key", "Value");
        map.put("Your_specific_key", "Value");
        map.put("Your_specific_key", "Value");
        your.add(Position_num, map);

for

/** * 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++;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.