我正在尝试使用电话号码和光标中的日期来填充ListView,但我不知道我做错了什么:
String[] cols = new String[]{ConcursoProvider.PARTICIPANTE_TELEFONO,ConcursoProvider.PARTICIPANTE_FECHA};
ConcursoProvider.tabla_Elegida = ConcursoProvider.TABLA_PARTICIPANTE;
Cursor c = getContentResolver().query(ConcursoProvider.CONTENT_URI,cols,null , null, null);
ListView lista = (ListView)findViewById(R.id.listaAcertantes);
int[] ids ={android.R.id.text1,android.R.id.text2};
lista.setAdapter(new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,c,cols,ids,0));
logcat告诉我这个:
08-22 07:47:02.951: E/AndroidRuntime(13442): FATAL EXCEPTION: main
08-22 07:47:02.951: E/AndroidRuntime(13442): Process: com.example.proyectosms, PID: 13442
08-22 07:47:02.951: E/AndroidRuntime(13442): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.proyectosms/com.example.proyectosms.AcertantesActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
08-22 07:47:02.951: E/AndroidRuntime(13442): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-22 07:47:02.951: E/AndroidRuntime(13442): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-22 07:47:02.951: E/AndroidRuntime(13442): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-22 07:47:02.951: E/AndroidRuntime(13442): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-22 07:47:02.951: E/AndroidRuntime(13442): at android.os.Handler.dispatchMessage(Handler.java:102)
08-22 07:47:02.951: E/AndroidRuntime(13442): at android.os.Looper.loop(Looper.java:136)
08-22 07:47:02.951: E/AndroidRuntime(13442): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-22 07:47:02.951: E/AndroidRuntime(13442): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 07:47:02.951: E/AndroidRuntime(13442): at java.lang.reflect.Method.invoke(Method.java:515)
08-22 07:47:02.951: E/AndroidRuntime(13442): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-22 07:47:02.951: E/AndroidRuntime(13442): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-22 07:47:02.951: E/AndroidRuntime(13442): at dalvik.system.NativeStart.main(Native Method)
08-22 07:47:02.951: E/AndroidRuntime(13442): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
08-22 07:47:02.951: E/AndroidRuntime(13442): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
08-22 07:47:02.951: E/AndroidRuntime(13442): at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:78)
08-22 07:47:02.951: E/AndroidRuntime(13442): at android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:174)
08-22 07:47:02.951: E/AndroidRuntime(13442): at android.support.v4.widget.CursorAdapter.<init>(CursorAdapter.java:151)
08-22 07:47:02.951: E/AndroidRuntime(13442): at android.support.v4.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:93)
08-22 07:47:02.951: E/AndroidRuntime(13442): at android.support.v4.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:89)
08-22 07:47:02.951: E/AndroidRuntime(13442): at com.example.proyectosms.AcertantesActivity.RellenarLista(AcertantesActivity.java:41)
08-22 07:47:02.951: E/AndroidRuntime(13442): at com.example.proyectosms.AcertantesActivity.onCreate(AcertantesActivity.java:30)
08-22 07:47:02.951: E/AndroidRuntime(13442): at android.app.Activity.performCreate(Activity.java:5231)
08-22 07:47:02.951: E/AndroidRuntime(13442): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-22 07:47:02.951: E/AndroidRuntime(13442): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-22 07:47:02.951: E/AndroidRuntime(13442): ... 11 more
我认为它要求我提供“_id”列,但我不想显示该值。
答案 0 :(得分:1)
根据文件,
要使用Cursor备份ListView,游标必须包含名为_ID的列。因此,前面显示的查询检索“words”表的_ID列,即使ListView不显示它。此限制还解释了为什么大多数提供商的每个表都有一个_ID列。
答案 1 :(得分:0)
您的表格缺少 _id 列。
在CREATE TABLE
查询中添加_id
列。
例如:
String CREATE_TABLE= " CREATE TABLE "+TABLE_NAME+" (_id INTEGER PRIMARY KEY,"+COLUMN1+" TEXT,"+COLUMN2+" TEXT,"+COLUMN3+" INTEGER)";
或
实施BaseColumns
,您可以在其中定义表格的所有列。
例如:
public static class Entry implements BaseColumns {
public final String idd;
public final String title;
public final String link;
public final String summary;
public final Long published;
private Entry(String id,String title, String summary, String link, long published) {
this.title = title;
this.summary = summary;
this.link = link;
this.idd=id;
this.published=published;
}
}
实施BaseColumns
后,它会自动将_id
列添加到您的表格中。
执行此操作时不要忘记更改数据库版本。