我试图在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
答案 0 :(得分:2)
您需要将从SharedPreferences
检索到的字符串拆分为String数组,以便在ListView中显示为:
String[] value=sharedUsername.split(",");
// Your code here...
无需在ArrayList
中存储SharedPreferences
,只需将日期存储在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....
}