2014-01-29 95 views
0

我有一個帶有按鈕的ListView。當我點擊按鈕時,彈出一個帶有EditText的AlertDialog。當用戶將數據輸入到AlertDialog的EditText中時,它會熄滅並更新SQLite數據庫。當原始ListView顯示備份時,它是空白的。當我退出應用程序並返回應用程序時,AlertDialog中輸入的數據就會顯示出來。 AlertDialog關閉後,我需要顯示新數據。AlertDialog關閉後無法刷新ListView

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

回答

0

試試這個....

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

我應該在哪裏輸入該代碼? – user579426

+1

當用戶將數據輸入到AlertDialog的EditText中時,數據就會熄滅。一旦數據庫得到更新,添加此代碼。 –

+0

這完全沒有用! – user579426

1

他們這樣,我想通了這一點是使用下面的方法

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

基本上是以很快AlertDialog關閉它觸發關閉此方法它出去並重新填充ListView

0

我在Activity文件中做了這樣的事情,其中​​撥號og是必需的。

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

每次關閉ALertDialog片段時都會調用OnDismissListener。