简体   繁体   中英

Android SimpleCursorAdapter and ListView from Database

I'm working with SimpleCursorAdapter and ListView to retrieve data from database. 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. The call to db.query, if it follows convention doesn't look correct.

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. Typically the parameters for query in a content provider are as follows:

The URI, projection - your list of columns where_string - such as 'USERNAME = ?' where_arguments_string_array - a value for each ? 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)

The where_string, where_arguments_string_array and sortorder if not used can be passed as 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.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM