Simran
Simran

Reputation: 633

Android Java: ArraylistMultiMap last value associated with the key is not getting deleted

In my Android program, I have declared a Multimap in one of my activities:-

Multimap<String, String> myMultimap = new ArrayListMultimap<String,String>();

which stores the names of books belonging to the customer who rented those books.

Here, the customer is the key and name of books are the multiple values belonging to that key.

I have a java class 'Books' which has the following method:-

  1. deleteBooks() to delete a specific value from among the many values associated with the key.

Here is the code:-

public ArrayList deleteBooks(String customer_name, String book_name)
{
    Iterator keyIterator=myMultimap.keySet().iterator();
    while(keyIterator.hasNext())
    {
        Object key =  keyIterator.next();
        Collecton cl1=myMultimap.get(customer_name);

        if(myMultimap.containsEntry(customer_name,book_name))
        {
            if(cl1.size()==1 && cl1.contains(book_name))
            {
                keyIterator.remove();
            }
            cl1.remove(book_name);
        }
    }

    al2=new ArrayList(cl1);
    return al2;
}

deleteBooks() return an array list to MainActivity which is displaying all book names in a list view belonging to a particular customer.

When the customer deletes one of the many book names displayed in the list view, then the book is getting deleted. However, the problem occurs when only one book is left in the key-value pair.

The last book in the map doesn't get deleted.

I have written the code to check if only one book is left in the map, then the entire key should be deleted, but still the last value in the key-value pair doesn't delete.

Is this issue related to Multimaps or am I going wrong somewhere in my code?

Upvotes: 0

Views: 481

Answers (2)

ashwani kumar
ashwani kumar

Reputation: 19

If I am not wrong then yo should try with

if(cl1.size()==0 && cl1.contains(book_name))
 {
  keyIterator.remove();
}

instead of

if(cl1.size()==1 && cl1.contains(book_name))
 {
  keyIterator.remove();
}

Upvotes: 1

halfer
halfer

Reputation: 20429

(Posted answer on behalf of the question author).

I have made the following changes:

public ArrayList deleteBooks(String customer_name, String book_name)
{
    if(myMultimap.containsEntry(customer_name,book_name))
    {
       int count=myMultimap.keys().count(customer_name);

       if(count==1)
       {
           myMultimap.removeAll(emailID);
           al2.clear();
       }
       else
       {
           myMultimap.remove(customer_name,book_name);
           cl1=myMultimap.get(customer_name);
           al2=new ArrayList(cl1);
       }
    }
    return al2;
}

Here is my activity 'A' which invokes deleteBooks():

public void onClick(DialogInterface dialog, int which)
        {
            arrList1=cd.deleteBooks(customer_name,book_name);
            if(arrList1.isEmpty())
            {
                arrList1.clear();
                lv1.setAdapter(null);
                arrayAdapter.notifyDataSetChanged();
                Toast.makeText(getActivity(), "You have no books in you collection", Toast.LENGTH_SHORT).show();
            }
            else {
                arrayAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, arrList1);
                lv1.setAdapter(arrayAdapter);
                Toast.makeText(getActivity(), book_name + " has been removed from your collection", Toast.LENGTH_SHORT).show();
            }
        }

And now, all elements are getting deleted.

Upvotes: 0

Related Questions