[英]Android SimpleCursorAdapter and ListView from Database
I'm working with SimpleCursorAdapter and ListView to retrieve data from database. 我正在使用SimpleCursorAdapter和ListView从数据库检索数据。 This is my activity class 这是我的活动课
public class TimelineActivity extends ListActivity {
LoginDataBaseAdapter loginDataBaseAdapter;
@SuppressWarnings("deprecation")
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.list_view);
LoginDataBaseAdapter db = new LoginDataBaseAdapter(this);
//loginDataBaseAdapter = loginDataBaseAdapter.open();
Cursor cursor = db.test();
startManagingCursor(cursor);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor,
new String[] {db.get()},
new int[] { android.R.id.text1 });
setListAdapter(adapter);
}
}
And this is database class. 这是数据库类。
public Cursor test() {
Cursor cursor = db.query(true, "LOGIN", new String[]{"USERNAME"}, null, null, null, null, null, null);
return cursor;
}
public String get(){
Cursor c = null;
String a = c.getString(c.getColumnIndex("USERNAME"));
return a;
}
and error messages 和错误消息
07-08 13:48:00.561: E/AndroidRuntime(14179): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.studio/com.example.studio.TimelineActivity}: java.lang.NullPointerException
07-08 13:48:00.561: E/AndroidRuntime(14179): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
07-08 13:48:00.561: E/AndroidRuntime(14179): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
07-08 13:48:00.561: E/AndroidRuntime(14179): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-08 13:48:00.561: E/AndroidRuntime(14179): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
07-08 13:48:00.561: E/AndroidRuntime(14179): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 13:48:00.561: E/AndroidRuntime(14179): at android.os.Looper.loop(Looper.java:130)
07-08 13:48:00.561: E/AndroidRuntime(14179): at android.app.ActivityThread.main(ActivityThread.java:3693)
07-08 13:48:00.561: E/AndroidRuntime(14179): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 13:48:00.561: E/AndroidRuntime(14179): at java.lang.reflect.Method.invoke(Method.java:507)
07-08 13:48:00.561: E/AndroidRuntime(14179): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
07-08 13:48:00.561: E/AndroidRuntime(14179): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
07-08 13:48:00.561: E/AndroidRuntime(14179): at dalvik.system.NativeStart.main(Native Method)
07-08 13:48:00.561: E/AndroidRuntime(14179): Caused by: java.lang.NullPointerException
07-08 13:48:00.561: E/AndroidRuntime(14179): at com.example.studio.LoginDataBaseAdapter.get(LoginDataBaseAdapter.java:217)
07-08 13:48:00.561: E/AndroidRuntime(14179): at com.example.studio.TimelineActivity.onCreate(TimelineActivity.java:25)
07-08 13:48:00.561: E/AndroidRuntime(14179): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-08 13:48:00.561: E/AndroidRuntime(14179): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
07-08 13:48:00.561: E/AndroidRuntime(14179): ... 11 more
please help. 请帮忙。
I don't see your table structure, so from what I can see you are using db.query incorrectly if you have setup a proper async task or content provider. 我没有看到您的表结构,因此从我可以看到,如果您已设置适当的异步任务或内容提供程序,则您使用的db.query错误。 The call to db.query, if it follows convention doesn't look correct. 如果遵循约定,对db.query的调用看起来不正确。
Cursor cursor = db.query(true, "LOGIN", new String[]{"USERNAME"}, null, null, null, null, null, null);
The first parameter for the query is the table name, you have a value of true. 查询的第一个参数是表名称,您的值为true。 Typically the parameters for query in a content provider are as follows: 通常,内容提供者中用于查询的参数如下:
The URI, projection - your list of columns where_string - such as 'USERNAME = ?' URI投影-您的列列表where_string-例如“ USERNAME =?” where_arguments_string_array - a value for each ? where_arguments_string_array-每个值? in the above where_string in respective order sortorder - either the column number, ie 1, or 2 OR the column name and sort order(ASC or DESC) 在上述where_string中,按各自的顺序进行排序-列号(即1或2)或列名和排序顺序(ASC或DESC)
The where_string, where_arguments_string_array and sortorder if not used can be passed as null. where_string,where_arguments_string_array和sortorder(如果未使用)可以作为null传递。
FYI, just in case Android likes to have a _id Integer auto increment as the first column, especially when using Listview, ExpandedListView, etc. That's how it correlates it screen view to the cursor via the list adapter. 仅供参考,以防万一Android喜欢将_id Integer自动递增作为第一列,特别是在使用Listview,ExpandedListView等时。这就是它通过列表适配器将屏幕视图与光标关联的方式。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.