通过 Android 对话框插入数据后 ListView 不更新?

发布于 2024-12-04 22:18:54 字数 3538 浏览 0 评论 0原文

大家好,我有 listview,它显示 sqlite 数据库中的项目列表。使用对话框将数据插入数据库。插入成功。但是,如果我在 DialogBox 上输入数据并单击“确定”按钮,则数据会插入到 db 中,但插入的数据不会显示在 ListView 中。

ListView 不刷新...我使用了 adapter.setNotifyOnChange(true);。但这不会对列表视图产生任何影响。

我在 logcat 上收到警告,

`09-16 15:01:10.215: INFO/dalvikvm(2066): Uncaught exception thrown by finalizer (will be discarded):
09-16 15:01:10.215: INFO/dalvikvm(2066): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@44c4be28 on PAYMENT_TABLE that has not been deactivated or closed
09-16 15:01:10.215: INFO/dalvikvm(2066):     at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
09-16 15:01:10.215: INFO/dalvikvm(2066):     at dalvik.system.NativeStart.run(Native Method)`

但我在 OnDestory() 中关闭了光标,

这是我的代码。

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    mySQLiteAdapter = new SQLiteAdapter(this);         
    mySQLiteAdapter.openToWrite();
    cursor = mySQLiteAdapter.queueAll();

    cursor.moveToFirst();
    while(!cursor.isAfterLast()) {          
        String mTitleRaw = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_PAYMENT_TYPE));
        al.add(mTitleRaw);
        cursor.moveToNext();
        }    

    adapterChanged();
}

@SuppressWarnings("unchecked")
private void adapterChanged() {
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_multiple_choice, al);

    adapter.setNotifyOnChange(true);
    setListAdapter(adapter);

    listContent = getListView();
    listContent.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    //Object o = this.getListAdapter().getItem(position);
}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    if (mySQLiteAdapter != null) {
        mySQLiteAdapter.close();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    menu.add(0, MENU_ADD, 0, "Add New");                
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch(item.getItemId()) {      
    case MENU_ADD:
        AlertMsg();         
        return true;        
    }
    return super.onOptionsItemSelected(item);
}

private void updateList(){
    cursor.requery();
    }   

public void AlertMsg() {
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Payment Type");
    alert.setMessage("Add field");
    final EditText mTxt = new EditText(this);
    alert.setView(mTxt);
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub              
            String mNewField = mTxt.getText().toString();
            //Toast.makeText(getApplicationContext(), "Item " + mNewField + " Added", Toast.LENGTH_LONG).show();
            mySQLiteAdapter.insert(mNewField);
            updateList();

            adapterChanged();
        }
    });
    alert.show();
}

这是有效的方法吗

......

任何人都可以提供一些建议......

谢谢......

hi all Am having listview which displays list of items from sqlite db. Using DialogBox am inserting data in DB. Insertion done successfully. But if i entered data on DialogBox and click ok button, the data inserted in db but the inserted data is not displaying in ListView.

ListView is not refreshing.... I hv used adapter.setNotifyOnChange(true);. But this wont take any effect on listview.

And I have got warning on logcat as

`09-16 15:01:10.215: INFO/dalvikvm(2066): Uncaught exception thrown by finalizer (will be discarded):
09-16 15:01:10.215: INFO/dalvikvm(2066): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@44c4be28 on PAYMENT_TABLE that has not been deactivated or closed
09-16 15:01:10.215: INFO/dalvikvm(2066):     at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
09-16 15:01:10.215: INFO/dalvikvm(2066):     at dalvik.system.NativeStart.run(Native Method)`

But i hv closed the cursor in OnDestory()

Here is my code.

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    mySQLiteAdapter = new SQLiteAdapter(this);         
    mySQLiteAdapter.openToWrite();
    cursor = mySQLiteAdapter.queueAll();

    cursor.moveToFirst();
    while(!cursor.isAfterLast()) {          
        String mTitleRaw = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_PAYMENT_TYPE));
        al.add(mTitleRaw);
        cursor.moveToNext();
        }    

    adapterChanged();
}

@SuppressWarnings("unchecked")
private void adapterChanged() {
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_multiple_choice, al);

    adapter.setNotifyOnChange(true);
    setListAdapter(adapter);

    listContent = getListView();
    listContent.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    //Object o = this.getListAdapter().getItem(position);
}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    if (mySQLiteAdapter != null) {
        mySQLiteAdapter.close();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    menu.add(0, MENU_ADD, 0, "Add New");                
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch(item.getItemId()) {      
    case MENU_ADD:
        AlertMsg();         
        return true;        
    }
    return super.onOptionsItemSelected(item);
}

private void updateList(){
    cursor.requery();
    }   

public void AlertMsg() {
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Payment Type");
    alert.setMessage("Add field");
    final EditText mTxt = new EditText(this);
    alert.setView(mTxt);
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub              
            String mNewField = mTxt.getText().toString();
            //Toast.makeText(getApplicationContext(), "Item " + mNewField + " Added", Toast.LENGTH_LONG).show();
            mySQLiteAdapter.insert(mNewField);
            updateList();

            adapterChanged();
        }
    });
    alert.show();
}

}

Are this efficient way....

Can anyone pls give some suggestions.....

Thanks......

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文