0

我正在尝试在SQLite数据库中搜索并在 ListView 中显示结果,但我无法获得我想要的结果。它返回类似于 {1222233333,0909098886} 的东西,这不是我想要的。结果应该是这样的,在 listView 上彼此下方:

1222233333

0909098886

以下代码在 DBAdapter 中。

public ArrayList<String> getMeterNUmber1(String Date) throws SQLException
{
    String[] whereArgs = new String[]{Date};

    Cursor mCursor = db.rawQuery("SELECT MeterNumber FROM " + SCAN_TABLE + " WHERE  Date = ?",whereArgs);

    ArrayList<String> results = new ArrayList<String>();
    while (mCursor.moveToNext()) {
        results.add(mCursor.getString(0));
    }

    return results;
}

在这个类中,我使用 sharedPreferences 来存储结果并将其传递给下一个类,如下所示:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
editor.putString("Shareusername",dbUser.getMeterNUmber1(ss).toStrin());
editor.commit();

这就是我试图在 ListView 中显示结果的方式:

String sharedUsername;
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
sharedUsername = settings .getString("Shareusername","null");

String [] value =  new String[] {sharedUsername};

listView = (ListView) findViewById(R.id.mylist);

pojoArrayList = new ArrayList<PojoClass>();
customerListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,value);
listView.setAdapter(customerListAdapter);

如果我做错了什么,请帮助我。

日志

01-18 21:17:44.405: E/AndroidRuntime(4249): FATAL EXCEPTION: main
01-18 21:17:44.405: E/AndroidRuntime(4249): java.lang.RuntimeException: Unable to start activity ComponentInfo{scann.barcode.scan/scann.barcode.scan.Result}: java.lang.NullPointerException
01-18 21:17:44.405: E/AndroidRuntime(4249):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-18 21:17:44.405: E/AndroidRuntime(4249):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-18 21:17:44.405: E/AndroidRuntime(4249):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-18 21:17:44.405: E/AndroidRuntime(4249):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-18 21:17:44.405: E/AndroidRuntime(4249):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 21:17:44.405: E/AndroidRuntime(4249):     at android.os.Looper.loop(Looper.java:137)
01-18 21:17:44.405: E/AndroidRuntime(4249):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-18 21:17:44.405: E/AndroidRuntime(4249):     at java.lang.reflect.Method.invokeNative(Native Method)
01-18 21:17:44.405: E/AndroidRuntime(4249):     at java.lang.reflect.Method.invoke(Method.java:511)
01-18 21:17:44.405: E/AndroidRuntime(4249):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-18 21:17:44.405: E/AndroidRuntime(4249):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-18 21:17:44.405: E/AndroidRuntime(4249):     at dalvik.system.NativeStart.main(Native Method)
01-18 21:17:44.405: E/AndroidRuntime(4249): Caused by: java.lang.NullPointerException
01-18 21:17:44.405: E/AndroidRuntime(4249):     at scann.barcode.scan.DBAdapter.getMeterNUmber1(DBAdapter.java:272)
01-18 21:17:44.405: E/AndroidRuntime(4249):     at scann.barcode.scan.Result.onCreate(Result.java:58)
01-18 21:17:44.405: E/AndroidRuntime(4249):     at android.app.Activity.performCreate(Activity.java:5104)
01-18 21:17:44.405: E/AndroidRuntime(4249):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-18 21:17:44.405: E/AndroidRuntime(4249):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-18 21:17:44.405: E/AndroidRuntime(4249):     ... 11 more
4

1 回答 1

3

您需要将检索SharedPreferences到的字符串拆分为字符串数组,以在 ListView 中显示为:

String[] value=sharedUsername.split(",");

// Your code here...

无需存储ArrayListSharedPreferences只需将日期存储在 SharedPreferences 中即可:

SharedPreferences settings =
    PreferenceManager.getDefaultSharedPreferences(
        getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
editor.putString("datestr",ss);
editor.commit();

现在,当您想在 ListView 中显示数据时,使用日期从数据库中检索数据:

String sharedUsername;
SharedPreferences settings =
    PreferenceManager.getDefaultSharedPreferences(this);
shareddate = settings .getString("datestr","null");

ArrayList<String> sharedUsernamearr=null;
if(shareddate !=null){
   sharedUsernamearr=dbUser.getMeterNUmber1(shareddate);
}
else{
  // Do your code here
}

listView = (ListView) findViewById(R.id.mylist);

pojoArrayList = new ArrayList<PojoClass>();
if(sharedUsernamearr !=null){
    customerListAdapter = new ArrayAdapter<String>(this,
                                  android.R.layout.simple_list_item_1,
                                  sharedUsernamearr);
    listView.setAdapter(customerListAdapter);
}
else{
    // Add your code here....
}
于 2013-01-18T20:23:32.240 回答