我有一个带有按钮的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;
}
}
我应该在哪里输入该代码? – user579426
当用户将数据输入到AlertDialog的EditText中时,数据就会熄灭。一旦数据库得到更新,添加此代码。 –
这完全没有用! – user579426