Here is the code which i use to load the spinner in the .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()");
}
}
Here is the code in the SQL.java to get the items from the database.
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;
}
this is how i add records from another activity .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;
The problem is when I insert a new record in the database it doesn’t show up in the spinner immediately, rather it shows it only after restarting the application. can someone help me fix this?
Try refreshing your activity by any one of the method
or
It will solve your issue.
Hope it helps