I am trying to populate a spinner with data from a database. The code is in a subroutine. Everything executes but the spinner is empty. If I copy the code to the "onCreate(Bundle saveInstanceState) it works fine. I am thinking I have context issue but can not figure it out.
Here is the subroutine:
public void showMatchNames (){
Cursor c;
String sMatchName = "MatchTable";
c = db.query(sMatchName, new String[]{KEY_ROWID, KEY_TITLE}, null, null, null, null, null);
if(c.moveToFirst()){
SimpleCursorAdapter MatchAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item, c, new String[]{KEY_TITLE}, new int[]{android.R.id.text1});
MatchAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spin2 = (Spinner)this.findViewById(R.id.spinner2);
spin2.setAdapter(MatchAdapter);
}
scrMatchView.setOnItemSelectedListener(new mySpinnerListener());
c.close(); // Done with Cursor so close it
}
There is data in the database, confirmed by exporting to SQliteMan and doing a query.
I can also pull the names from the database and populate the spinner with a simple ArrayAdapter from the "showMatchNames" subroutine.
I am pretty sure it is something simple but I have been staring at this for hours now and can't get it to work.
Any help would be very welcome.
Related
i'am getting my Listview data from an sqlite database like this
ArrayAdapter<String> aa;
ArrayList<String> arrayList = new ArrayList<String>();
aa = new ArrayAdapter<String>(getActivity(),
R.layout.row_listview, arrayList);
setListAdapter(aa);
Cursor cursor8 = db.rawQuery("SELECT * FROM test21", null);
if(cursor8.getCount() == 0)
{
textView5.setVisibility(View.VISIBLE);
}cursor8.close();
Cursor cursor = db.rawQuery("SELECT * FROM test21", null);
// Toast.makeText(myContext, ""+cursor.getCount(), Toast.LENGTH_LONG).show();
if(cursor.moveToFirst())
{
do {
arrayList.add(cursor.getString(3));
}
while (cursor.moveToNext());
}
When i click another button it remove all the data of the sqlitedatabase.
How can i refresh the content of the listview because there is now nothing. I always need to go back in the activity each time :(
Get rid of the notifyDataSetChanged() and call requery() on your Cursor. Here is a sample project demonstrating this.
Hope it will works for you.
I'm having a lot of problems trying to get my spinner to fill with data after my application loads. I'm just messing around and made a database for vehicles. So I'm trying to get a spinner to load the database data into it. This is the code I use in a method to attempt to do so, but my spinner is never loaded with my database data.
SQLiteDatabase db = database.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT vehicle_make, vehicle_model, vehicle_year FROM vehicles;", null);
vehicleProfileSpinner = (Spinner) findViewById(R.id.vehicleProfileSpinner);
startManagingCursor(cursor);
String[] columns = {"vehicle_year", "vehicle_make", "vehicle_model"};
int[] theView = {R.id.vehicleProfileSpinner};
SimpleCursorAdapter vpsAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_dropdown_item, cursor, columns, theView);
vpsAdapter.setDropDownViewResource(R.layout.main);
vehicleProfileSpinner.setAdapter(vpsAdapter);
In my case instead of using Simplecursoradapter, I have created array from cursor entries and created ArrayAdapter and set it to list. It worked perfect. Somehow Simplecursor adapter looks for ID field and causing issue,so I ignored it and used Arrayadapter. I will try to update this post with example I posted for other question.
One thing is all you need SimpleCusorAdapter.
What am I doing wrong!?!??
I am trying to get a set of data to a listview.
First I Open the database and then I trying to get the set,
I get a response but only 1 row from the database and I am getting at least 10 when I trying it in sqlite browser.....
Anyway I don't know if this make any sense, here is the code: (i am new at this, please don't laugh to much)
And btw I use the same methods/functions in another listview but then I don't have any WHERE in my query and that works fine....
So I want to get all the rows from the database and i only get the first one :)
Thanks mates!
db.openDataBase();
Cursor c = db.getCoursesFromCountyID(countyID);
BindsimpleCursorAdapter(c);
db.close();
the getCoursesFromCountyID =
int id = Integer.parseInt(county);
return db.query("Courses", new String[]{KEY_course_ID, KEY_course}, KEY_county_ID + " = " + id, null, null, null, null);
And the BindsimpleCursorAdapter looks like this =
startManagingCursor(c);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.simpleadapter_courses, c, new String[]{DataBaseHelper.KEY_course_ID, DataBaseHelper.KEY_course}, new int[]{R.id.courseID, R.id.course});
ListView lv = new ListView(this);
lv = (ListView)findViewById(R.id.listViewCourses);
lv.setAdapter(adapter);
I don't see any problems on your code, try adding 'KEY_county_ID' to you projection argument and removing the selection argument to check if all the rows are really there.
i have been trying to bind spinner to a database and finally got succeeded. i have connected spinner to database table using cursoradapter. but the problem is the spinner gets populated but the list items in it shows blank text. i know binding is successful because it shows as many rows as there are records in database table.
can not figure out what it is. some body please help i am stuck here
i am posting the code below
public long createAccount() {
ContentValues initialValues = createContentValues();
Log.i("DB", initialValues.get(KEY_NAME)+":"+
initialValues.get(KEY_MAILBOXTYPE)+":"+
initialValues.get(KEY_OUTPORT)+":"+
initialValues.get(KEY_INPORT)+":"+
initialValues.get(KEY_INSERVER)+":"+
initialValues.get(KEY_OUTSERVER)+":");
return database.insert(DATABASE_TABLE, null, initialValues);
}
/**
* Return a Cursor over the list of all todo in the database
*
* #return Cursor over all notes
*/
public Cursor fetchAccount() {
Log.i("DB", "Cursor opened");
return database.query(DATABASE_TABLE, new String[] { KEY_ROWID,
KEY_NAME,KEY_INSERVER}, null, null, null,
null, null);
}
Code for spinner binding is below:
mAccAdap.createAccount();
Cursor c = mAccAdap.fetchAccount();
startManagingCursor(c);
SimpleCursorAdapter CursorAdapter = new SimpleCursorAdapter(
this,android.R.layout.simple_spinner_item,c,
new String[]{mAccAdap.KEY_INSERVER},new int[]{R.id.tvDBViewRow});
CursorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinEmail=(Spinner)findViewById(R.id.spinAccount);
spinEmail.setAdapter(CursorAdapter);
Try changing this part of your SimpleCursorAdapter...
new int[]{R.id.tvDBViewRow}
to this...
new int[]{android.R.id.text1}
I'm trying to load the contents of my SQLite table into a ListView using the following code.
myDbHelper = new DatabaseHelper(this);
Cursor cursor = getData();
CursorAdapter dataSource = new SimpleCursorAdapter(this, R.layout.menu_item, cursor, fields, null);
menuList.setAdapter(datasource); //My ListView
private Cursor getData() {
SQLiteDatabase db = myDbHelper.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null,null, ORDER_BY);
return cursor;
}
When I run the app it passes through this code and then throws an error. Am I using the correct technique for adding data to a ListView?
You need to give a little more information.
Where does it crash?
What is the error that it gives?
You are using adapt but you do not declare it or set it to anything in your code snippet - if it's null, then that will be a problem.
menuList.setAdapter(adapt); //My ListView
Take a look at my example code here for how I use ListViews with SQLite.