我很难用简单的游标适配器从sqlite数据库填充微调器。我必须使用一个简单的游标适配器,而不是数组适配器。我的主要活动、函数和xml文件如下:
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);
..............
..............
}
功能如下:
public Cursor getCursor() {
Cursor c = database.rawQuery("select * from " + Database.mylist + " where isCancel = 0", null);
return c;
}
xml文件在下面;
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" />
当我运行应用程序时,将显示以下屏幕。有数据,但似乎是空的。我有_ id,列的名称。
空微调器
谢谢你的帮助。
改变
int[] to = new int[] { R.id.spinner };
到
int[] to = new int[] { android.R.id.text1 };
你可以在这里阅读更多关于SimpleCursorAdapter的信息:Android:使用SimpleCursorAdapter将数据从数据库获取到ListView。
您还使用Database.mylist作为列名和表名,这是PPartisan在注释中指出的。