0

I'm creating a method to read all the information in the database and view it through a spinner here is the code i tried for this function

public Spinner loadArtist(){
        SQLiteDatabase DB = getReadableDatabase();

        String[] projection = {
                ArtistMaster.Artist.ARTIST_NAME};

        Cursor cursor = DB.query(
                ArtistMaster.Artist.TABLE_ARTIST,
                projection,
                null,
                null,
                null,
                null,
                null);

        Spinner itemIds = new ArrayList<>();
        while(cursor.moveToNext()) {
            long itemId = cursor.getLong(
                    cursor.getColumnIndex(ArtistMaster.Artist.ARTIST_NAME));
            itemIds.setAdapter(itemId);
        }
        cursor.close();
        return itemIds;
    }

but it gives me an error in this line Spinner itemIds = new ArrayList<>(); Should i declare it as a list instead of spinner

2 Answers2

0

itemIds should be defined as an ArrayList<Long>. A Spinner is a UI element and an ArrayList is a data structure. You will most likely need to map the data to your UI using an adapter of some sort, eg. an ArrayAdapter:

Spinner spinner = ... // findViewById, new Spinner() etc.
ArrayList<Long> itemIds = new ArrayList<>();
//... fill array with artist IDs

spinner.setAdapter(
    new ArrayAdapter(
        this, // Context, Activity etc.,
        android.R.layout.simple_list_item_1, // Spinner TextView item resource ID
        itemIds // Data set. 
));

By default, ArrayAdapter will call Object#toString() on each data object in the collection.

Sandi
  • 2,415
  • 1
  • 13
  • 11
0

I'd suggest that it would be easier if you used a Cursor Adpater as they are designed to be used with a Cursor and there is no need to generate arrays.

SimpleCursorAdapter being that, a simple but still pretty flexible adapter for use with Cursors.

The only issue is that a Cursor Adapter requires a column name specifically _id (BaseColumns._ID resolves to this (as used below)).

  1. First have the following member variables (obviously names can be what you wish)

:-

Cursor mCursor;
SimpleCursorAdapter mAdapter;
Spinner spinner;
SQLiteDatabase db;
  1. In the onCreate Method of the activity have

:-

spinner = this.findViewById(R.id.?????); //
db = ???????? (as per your existing code)
manageSpinner();
  1. Have a method

:-

private void manageSpinner() {
    mCursor = db.query(
            ArtistMaster.Artist.ARTIST_NAME,
            new String[]{"*","rowid AS " + BaseColumns._ID}, //<<<<<<<< adds _ID column (unless the table is a WITHOUT ROWID table, pretty unlikely)
            null,null,null,null,null
    );
    if (mAdapter == null) {
        mAdapter = new SimpleCursorAdapter(
                this,
                android.R.layout.simple_list_item_1,
                mCursor,
                new String[]{"the_column"}, // column(s) from which to extract data
                new int[]{android.R.id.text1}, // layout views into which the data is placed
                0
        );
        spinner.setAdapter(mAdapter);
        // You want want to do something when an Item in the spinner is clicked (this does nothing as it is)
        spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //........... do your stuff here
                // notes
                // id will be the id of the row 
                // cursor will be positioned, so you can access data from the cursor if needed
            }
        });
    } else {
        mAdapter.swapCursor(mCursor);
    }

}
  1. Override the activity's onResume (refresh the spinner when returning to activity as underlying data may have changed) and onDestroy (to close the Cursor) methods using

:-

@Override
protected void onDestroy() {
    super.onDestroy();
    mCursor.close();
}

@Override
protected void onResume() {
    super.onResume();
    manageSpinner();
}
MikeT
  • 51,415
  • 16
  • 49
  • 68
  • This is not right i cant call the find view by id in the DBHandler class – Group Assignment Oct 14 '19 at 15:56
  • @GroupAssignment you don't typically put the spinner in the DBHandler class you put it in the activity which gets an instance of the DBHanlder. The DBHanlder should be for handling the Database. So in the **activity** you'd have a class member variable for the handler e.g. `DBHandler mDBHandler;` in the onCreate you instantiate the DBHandler e.g. `mDBHandler = new DBHandler(this);` and then you could use `db = DBHandler.getWritableDatabase();` and thus db is an SQLiteDatabase instance. – MikeT Oct 14 '19 at 18:59