Mamta Kaundal
Mamta Kaundal

Reputation: 453

Updating hashMap value on specific position

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

Upvotes: 0

Views: 7110

Answers (4)

Hassan Badawi
Hassan Badawi

Reputation: 302

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

Upvotes: 0

peeyush pathak
peeyush pathak

Reputation: 3663

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.

Upvotes: 0

Sasi Kumar
Sasi Kumar

Reputation: 13358

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.

Upvotes: 0

Rajen Raiyarela
Rajen Raiyarela

Reputation: 5634

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.

Upvotes: 1

Related Questions