创建新项目后重新加载listView

时间:2014-04-15 15:27:01

标签: android sqlite listview

我正在尝试在将新数据添加到数据库之后使我的应用程序返回到listview活动。但每当我尝试调用notifyDataSetChanged()命令时,应用程序崩溃。

主要活动是:

Button createTerm;
ListView termList;
TextView emptyList;
Cursor cursor;
SQLiteDatabase db = null;
ListViewAdapter termListAdapter = null;

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

    db = new DatabaseHelper(this).getWritableDatabase();

    createTerm = (Button) findViewById(R.id.btn_createTerm);
    termList = (ListView) findViewById(R.id.lv_termList);
    // What when there are no terms.
    termList.setEmptyView((TextView) findViewById(R.id.tv_emptyTermList));

    // Create the cursor to populate the listview;
    cursor = db.rawQuery("SELECT * FROM " + DatabaseHelper.TABLE_TERM, null);

    // Options for the on Term View Click Menu
    final String options[] = { "View", "Delete" };
    final ArrayAdapter<String> termMenuAdapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, options);
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);

    // Set a cursor adapter to my list.
    termListAdapter = new ListViewAdapter(this, cursor, false, 1);
    termList.setAdapter(termListAdapter);

    // If I click on the "New Term" button.
    createTerm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Context c = v.getContext();
            Intent i = new Intent(c, CreateTermWindow.class);
            startActivityForResult(i, 1);
        }
    });

// When we finish creating a new Term.
@Override
protected void onActivityResult(int request, int result, Intent data) {
    if(request == 1){
        termListAdapter.notifyDataSetChanged();
    }
}

第二个活动代码:

tcreate.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            SQLiteDatabase db = new DatabaseHelper(getBaseContext()).getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("name", tname.getText().toString());
            values.put("start", tstart.getText().toString());
            values.put("end", tend.getText().toString());
            db.insert("termTable", null, values);
            Toast.makeText(getApplicationContext(), "New Term Created!", Toast.LENGTH_LONG).show();
            setResult(0);
            finish();
        }
    });

logcat的:

04-15 12:35:08.509: W/dalvikvm(14846): threadid=1: thread exiting with uncaught exception (group=0x40da61f8)
04-15 12:35:08.529: E/AndroidRuntime(14846): FATAL EXCEPTION: main
04-15 12:35:08.529: E/AndroidRuntime(14846): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:400)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at com.dagonar.achievegpa.helperclass.ListViewAdapter.bindView(ListViewAdapter.java:46)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:256)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.widget.AbsListView.obtainView(AbsListView.java:2460)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.widget.ListView.makeAndAddView(ListView.java:1772)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.widget.ListView.fillDown(ListView.java:672)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.widget.ListView.fillFromTop(ListView.java:732)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.widget.ListView.layoutChildren(ListView.java:1611)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.widget.AbsListView.onLayout(AbsListView.java:2290)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.view.View.layout(View.java:11338)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:980)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.view.View.layout(View.java:11338)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.view.View.layout(View.java:11338)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1658)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1516)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1429)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.view.View.layout(View.java:11338)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.view.View.layout(View.java:11338)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1493)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2452)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.os.Looper.loop(Looper.java:137)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.app.ActivityThread.main(ActivityThread.java:4464)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at java.lang.reflect.Method.invokeNative(Native Method)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at java.lang.reflect.Method.invoke(Method.java:511)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:822)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:589)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

在尝试从中读取数据之前,您是否致电cursor.moveToFirst() cursor.moveToPosition(...)了?从您的logcat:

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:400)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
04-15 12:35:08.529: E/AndroidRuntime(14846):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)

答案 1 :(得分:0)

调用adapter.notifyDataSetChanged();