I have spent days to figure this out but no luck. Wish I could get answer from here. I tried to load data into spinner from my content provider using the cursorLoader method. The spinner seem had received the data but I found no data in the dropdown list, although several dropdown items (with no text) had been created.
I believe problem is not from my provider because if I use the same cursor to retrieve the data and put it into array, then bind the array to the spinner, then it shows all items correctly.
Below is my code,
package com.supreme2u.shopper;
import android.app.Activity;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import com.supreme2u.shopper.provider.ShopperProvider;
public class RecordActivity extends Activity implements LoaderManager.LoaderCallbacks<Cursor> {
private SimpleCursorAdapter sAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record);
getLoaderManager().initLoader(0, null, this);
sAdapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_spinner_item,
null,
new String[] {ShopperProvider.TAG_COLUMN_TAG},
new int[] {R.id.spinner1},
0);
sAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
Spinner v = (Spinner) findViewById(R.id.spinner1);
v.setAdapter(sAdapter);
}
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
CursorLoader cursorLoader = new CursorLoader(
this,
ShopperProvider.CONTENT_URI_TAGS,
ShopperProvider.TAG_COLUMNS,
null,
null,
null);
return cursorLoader;
}
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
sAdapter.swapCursor(arg1);
}
public void onLoaderReset(Loader<Cursor> arg0) {
sAdapter.swapCursor(null);
}
}
And my layout xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
And from my ShopperProvider class, extracted,
public static final Uri CONTENT_URI_TAGS = Uri.parse("content://com.supreme2u.shopper.provider/tableTag");
public static final String TAG_COLUMN_ID = "_id";
public static final String TAG_COLUMN_TAG = "tagName";
public static final String[] TAG_COLUMNS = {"_id","tagName"};
The problem lies in your construction of the SimpleCursorAdapter, specifically the second and fifth parameters (layout and to). From the docs:
So you are correct to use android.R.layout.simple_spinner_item for layout. However, the views passed for to need to be TextViews and contained within android.R.layout.simple_spinner_item: i.e. android.R.id.text1 instead of R.id.spinner1.
In short, use this construction instead: