这是我用来在.MainActivity.java中加载微调器的代码
public void spin() {
{
try{
SQL db = new SQL(getBaseContext());
db.open();
List<String> cursor = db.selectAll();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, cursor);
s.setAdapter(adapter);
}
catch(Exception e)
{
Log.v("Error","e.tostring()");
}
}
这是SQL.java中用于从数据库中获取项目的代码。
public List<String> selectAll() {
List<String> list = new ArrayList<String>();
Cursor cursor = this.mydb.query(DATABASE_TABLE, new String[] {KEY_NAME
}, null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
} while (cursor.moveToNext());
}
if (cursor !=null && !cursor.isClosed()) {
cursor.close();
}
return list;
}
这就是我从另一个活动添加记录的方式 .Addnew.java
case R.id.bsave:
boolean work=true;
try{
String rno1 = rno.getText().toString();
String name1 = name.getText().toString();
String reg1 = reg.getText().toString();
String mob1 = mob.getText().toString();
SQL entry = new SQL(Addnew.this);
entry.open();
entry.createEntry(rno1,name1,reg1,mob1);
entry.close();
}
catch(Exception e){
work=false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Naah");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}finally{
if(work){
Dialog d = new Dialog(this);
d.setTitle("Yeah");
TextView tv = new TextView(this);
tv.setText("Success");
d.setContentView(tv);
d.show();
}
}
break;
问题是当我在数据库中插入一条新记录时,它不会立即显示在微调器中,而是仅在重新启动应用程序后才显示。有人可以帮我解决这个问题吗?