1

I have some trouble to populate spinner from sqlite database with simple cursor adapter. I must use simple cursor adapter not array adapter. My MainActivity, functions and xml files are as follows:

  public class MainActivity extends BaseActivity {


    private Spinner workerId = (Spinner) findViewById(R.id.spinner);

    c = getCursor();

    String[] columns = new String[]{Database.mylist};
    int[] to = new int[] { R.id.spinner };
    myAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, c, columns,to, 0);
    workerId.setAdapter(myAdapter);

    ..............
    ..............
    }

Function is below:

    public Cursor getCursor() {
    Cursor c = database.rawQuery("select * from " + Database.mylist + " where isCancel = 0", null);
    return c;
}

Xml file is below;

        MainActivity.xml


    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="5dp"
        android:layout_height="wrap_content" />

And when I run app the following screen displays. There is data but it seems empty. I have _id, name columns.

empty spinner

Thank you for your help.

Worker
  • 23
  • 4
  • 1
    Is `Database.myList` pointing to the name of a table, or the name of a column? Because you use it as both. Have you checked to see whether your `getCursor()` method returns a `Cursor` with any rows? – PPartisan Jul 02 '17 at 20:22

1 Answers1

1

Change

int[] to = new int[] { R.id.spinner };

to

int[] to = new int[] { android.R.id.text1 };

And you can read a little bit more about SimpleCursorAdapter here: Android: Using SimpleCursorAdapter to get Data from Database to ListView

Also you are using Database.mylist as column name and table name as PPartisan pointed out in a comment.

Josef Adamcik
  • 5,620
  • 3
  • 36
  • 42
  • String[] columns = new String[]{Database.mylist}; will be String[] columns = new String[]{Database.col_name}; I did write it wrong. But it does not solve problem. – Worker Jul 02 '17 at 20:56
  • Thanks Josef. Now it Works. – Worker Jul 02 '17 at 23:09