-2

Method of add gives error for ArrayList. It isn't adding.

    public List<String> arrayList;

...

arrayList = new ArrayList<String>();

...

savenumberButton.setOnClickListener(new View.OnClickListener()
    {

        public void onClick(View v)
        {
            String str = numberText.getText().toString();
            Integer cout = listView.getCount()+ 1;
            String str1 = cout.toString().concat("."+str);

            try {
                arrayList.add(listView.getCount(), str1);
            } catch (Exception e) {
                Log.w("Error", "arraylist.add is not running!");
            }

            arrayAdapter.notifyDataSetChanged();
            Toast.makeText(getApplicationContext(), "Data Saved", Toast.LENGTH_SHORT).show();
            blockNumberText.setText(" ");
        }
    });

...
5
  • 1
    Might need to use arrayList.add(listView.getCount() - 1, str1);
    – stkent
    Commented Feb 11, 2015 at 21:29
  • What is the returned data type of listView.getCount()?
    – chsh
    Commented Feb 11, 2015 at 21:30
  • 1
    Two things: tell us what the error is, and tell us what listView is.
    – Jeff G
    Commented Feb 11, 2015 at 21:33
  • 2
    Call e.printStackTrace() on catch block and publish logs from error Commented Feb 11, 2015 at 21:33
  • ... W/System.err﹕ java.lang.UnsupportedOperationException ... W/System.err﹕ at java.util.AbstractList.add(AbstractList.java:404) ... W/System.err﹕ at com......MainActivity$1.onClick(MainActivity.java:78) ... W/System.err﹕ at android.view.View.performClick(View.java:4633) ... W/System.err﹕ at android.view.View$PerformClick.run(View.java:19330) Commented Feb 11, 2015 at 22:01

1 Answer 1

6

Now that you have posted the exception and line number and more details in the comments, this seems be because when you call asList() This method "Returns a fixed-size list backed by the specified array.." then you just cast it to an ArrayListwhich is incorrect.

so the method add throws the UnsupportedOperationException because the List has a fixed size and cannot be modified.

instead try

arrayList = new ArrayList<String> (Arrays.asList(TextUtils.split(number, ",")));
7
  • I didn't understand an index that already exists. Could you please explain it? Commented Feb 11, 2015 at 22:19
  • That's not the error you are getting but if you have an ArrayList like <item0,item1,item2,item3> and you call ArrayList.add(5, item4), you will get an IndexOutOfBounds Exception.
    – Dave S
    Commented Feb 11, 2015 at 22:20
  • Line 78 is arrayList.add(listView.getCount() - 1, str1); Commented Feb 11, 2015 at 22:21
  • I just changed as the AbstractList but same line is problem W/System.err﹕ at .....MainActivity$1.onClick(MainActivity.java:79) Commented Feb 11, 2015 at 22:31
  • AbstractList is not the right class. It is an Abstract class and is meant to be extended by other classes and the methods are implemented there. Use ArrayList
    – Dave S
    Commented Feb 11, 2015 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.