AlertDialog关闭后无法刷新ListView

时间:2014-01-29 22:00:29

标签: android sqlite android-listview

我有一个ListView,上面有一个按钮。当我单击Button时,我会弹出一个带有EditText的AlertDialog。当用户将数据输入AlertDialog上的EditText时,它会退出并更新SQLite数据库。当原始ListView显示备份时,它是空白的。当我退出应用程序并返回应用程序时,将显示AlertDialog中输入的数据。我需要在AlertDialog关闭后显示新数据。

package com.wmason.testcreator;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.os.Bundle;
//import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.app.ListActivity;
import android.widget.Button;
import android.content.Intent;
import android.content.res.AssetManager;
import android.database.Cursor;

public class MainActivity extends ListActivity {
    private DbManagement mdbManager;
    private TestProcessor tp;
    SimpleCursorAdapter notes;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lookup);

        mdbManager = new DbManagement(this);
        tp = new TestProcessor(this); 
        mdbManager.open();
        fillData();
        Button testingCsv =(Button)findViewById(R.id.btnTestCsv);
        testingCsv.setOnClickListener(ChokeSlam);
        fillData();      
    }




    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        fillData();
    }

    private OnClickListener ChokeSlam = new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            AssetManager aM = getAssets();

            try{                
                //This line of code opens the AlertDialog
                tp.ProcessInboundStream(aM,"Book1.csv",mdbManager);
                fillData();     
            }
            catch(Exception ex){
              System.out.println(ex.toString());
            }
        }       
    };

    protected void onListItemClick(ListView l, View v, int position, long id) {
        /*
        boolean b;
        b=mdbManager.deleteTests(id);
        */
        //fillData();
        Intent i = new Intent(this,DisplayTests.class);

        i.putExtra("ID",Long.toString(id));

        startActivity(i);
    }
    private void fillData(){
        Cursor testCursor = mdbManager.fetchAllTests();
        startManagingCursor(testCursor);
        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{DbManagement.Gen_Test_Name};

        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.text1};
//R.layout.
        // Now create a simple cursor adapter and set it to display
        notes = 
            new SimpleCursorAdapter(this, R.layout.testrows, testCursor, from, to);

        setListAdapter(notes);
        notes.notifyDataSetChanged();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

3 个答案:

答案 0 :(得分:1)

我认为这样做是为了使用以下方法

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
    fillData();  
}

基本上,一旦AlertDialog关闭,就会触发此方法并将其关闭并重新填充ListView

答案 1 :(得分:0)

试试这个......

listview.setAdapter(adapter);
adapter.notifyDataSetChanged();

答案 2 :(得分:0)

我在需要对话框的Activity文件中执行类似的操作。

AlertDialog dialog = dialogViewer.returnEditDialog();
dialog.show();
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            refreshView();
                        }
                    });

每次关闭ALertDialog片段时都会调用OnDismissListener。