0

I'm creating an app that displays a clickable ListView that allows the user to operate on corresponding entries in a SQL table on click.

public class MainActivity extends AppCompatActivity {

private DBAdapter db;
private LinkedList<Book> ll;
private String[] nameArray;
private ListView listView;
private int BOOK_RECORD_REQUEST = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Force table drop if exists
    db = new DBAdapter(this);
    db.dropTable();

    //Create new table
    db = new DBAdapter(this);
    db.open();

    //Parse Text File

    defaultTable();

    ll = db.getAllBooks();
    nameArray = new String[ll.size()];

    for (int i=0; i < ll.size(); i++){
        nameArray[i] = ll.get(i).getTitle();
    }

    listView = (ListView) findViewById(R.id.list);
    ArrayAdapter aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, nameArray);

    listView.setAdapter(aa);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int clicked_index, long l) {
            Intent intent = new Intent("j.cubed.booklibrary.BookActivity");
            int usr_select_id = ll.get(clicked_index).getID();
            intent.putExtra("id", usr_select_id);
            startActivityForResult(intent, BOOK_RECORD_REQUEST);
        }
    });

After operation on the table, onActivityResult() is used to pull a new list of the data objects from the current SQL table, and I need to update the adapter to reflect such (to handle cases an item is deleted or added).

public void onActivityResult(int requestCode, int resultCode, Intent intent){
    if (requestCode == BOOK_RECORD_REQUEST){
        if (resultCode == RESULT_OK){
            ll = db.getAllBooks();
            ArrayAdapter aa = (ArrayAdapter) listView.getAdapter();
            for (int i=0; i < ll.size(); i++){
                nameArray[i] = ll.get(i).getTitle();
            }
            aa.clear();
            aa.addAll(nameArray);
            listView.setAdapter(aa);
        }
    }
}

Upon reaching 'aa.clear()', the program cashes.

11-12 15:58:49.521 2331-2331/j.cubed.booklibrary E/AndroidRuntime: FATAL EXCEPTION: main
                                                               Process: j.cubed.booklibrary, PID: 2331
                                                               java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {j.cubed.booklibrary/j.cubed.booklibrary.MainActivity}: java.lang.UnsupportedOperationException
                                                                   at android.app.ActivityThread.deliverResults(ActivityThread.java:4089)
                                                                   at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)
                                                                   at android.app.ActivityThread.-wrap20(ActivityThread.java)
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                Caused by: java.lang.UnsupportedOperationException
                                                                   at java.util.AbstractList.remove(AbstractList.java:161)
                                                                   at java.util.AbstractList$Itr.remove(AbstractList.java:374)
                                                                   at java.util.AbstractList.removeRange(AbstractList.java:571)
                                                                   at java.util.AbstractList.clear(AbstractList.java:234)
                                                                   at android.widget.ArrayAdapter.clear(ArrayAdapter.java:285)
                                                                   at j.cubed.booklibrary.MainActivity.onActivityResult(MainActivity.java:83)
                                                                   at android.app.Activity.dispatchActivityResult(Activity.java:6932)
                                                                   at android.app.ActivityThread.deliverResults(ActivityThread.java:4085)
                                                                   at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132) 
                                                                   at android.app.ActivityThread.-wrap20(ActivityThread.java) 
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533) 
                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                   at android.os.Looper.loop(Looper.java:154) 
                                                                   at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
sh34v3
  • 301
  • 2
  • 9
  • Why try to get the ArrayAdapter from the ListView? A better approach would be to declare the ArrayAdapter as a field and just access it in onActivityResult(). – Ivan Wooll Nov 12 '17 at 21:38
  • Here the cause of your problem is explained: https://stackoverflow.com/questions/3200551/unable-to-modify-arrayadapter-in-listview-unsupportedoperationexception – algrid Nov 12 '17 at 21:52
  • @algrid 's comment lead to the solution. Thanks! – sh34v3 Nov 12 '17 at 22:12

0 Answers0