在onActivityResult()上运行ListView数组适配器会导致应用程序崩溃

时间:2017-11-12 21:29:45

标签: android listview onactivityresult

我正在创建一个显示可点击ListView的应用程序,该应用允许用户在点击时对SQL表中的相应条目进行操作。

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

在对表进行操作之后,onActivityResult()用于从当前SQL表中提取新的数据对象列表,我需要更新适配器以反映这种情况(处理项目被删除或添加的情况)

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

达到'aa.clear()'后,程序会兑现。

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) 

0 个答案:

没有答案