hi all i need to populate database elements in listview. i get null pointer exception and array adapter errors. i did the following ways: 1)created a string array with size of number of rows 2) stored database values in array 3)merge in listview . i tried long time but i cannot find it. in coding:
Cursor c = db.rawQuery("select * from stopwatch" , null);
if (c != null ) {
if (c.moveToFirst()) {
do {
counter=counter+1; // finding no of rows in table.
}while (c.moveToNext());
}
}
array=new String[counter];
counter1=0;
Cursor c1 = db.rawQuery("select * from stopwatch" , null);
if (c1 != null ) {
if (c1.moveToFirst()) {
do {
array[counter1]=String.valueOf(c1.getInt(0))+":"+String.valueOf(c1.getInt(1))+":"+String.valueOf(c1.getInt(2));
counter=counter+1; }while (c1.moveToNext()); } } db.close();
// populating in listview as follows
list1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array));
my logcat:
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): java.lang.NullPointerException
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:351)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.widget.AbsListView.obtainView(AbsListView.java:1254)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.widget.ListView.measureHeightOfChildren(ListView.java:1142)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.widget.ListView.onMeasure(ListView.java:1055)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.view.View.measure(View.java:7117)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2929)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:619)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.widget.LinearLayout.onMeasure(LinearLayout.java:280)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.view.View.measure(View.java:7117)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2929)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.view.View.measure(View.java:7117)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.view.View.measure(View.java:7117)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2929)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.view.View.measure(View.java:7117)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.view.ViewRoot.performTraversals(ViewRoot.java:713)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.view.ViewRoot.handleMessage(ViewRoot.java:1504)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.os.Looper.loop(Looper.java:123)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at android.app.Act开发者_运维技巧ivityThread.main(ActivityThread.java:3948)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at java.lang.reflect.Method.invoke(Method.java:521)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
05-12 18:52:30.853: ERROR/AndroidRuntime(8330): at dalvik.system.NativeStart.main(Native Method)
Try using the SQLiteOpenHelper to create a datahelper
implement a function like so:
public List<List<String>> getData(){
Cursor cursor = this.db.query(TABLE_NAME,new String[] { "value1", "value2" }, null, null, null, null, "code asc");
List<List<String>> list = new ArrayList<List<String>>();
if (cursor.moveToFirst()){
do{
List<String> temp = new ArrayList<String>();
temp.add(cursor.getString(0));
temp.add(cursor.getString(1));
list.add(temp);
}while (cursor.moveToNext());
}
return list;
}
then in the other class call:
private List<List<String>> liItems;
liItems = dh.getData();
/** create an array in the proper length */
String[] rgItems = new String[liItems.size()];
/** Fill the array with data */
for (int i=0; i < liItems.size(); i++){
List<String> temp = liItems.get(i);
rgItems[i] = temp.get(0) + " - " + temp.get(1);
}
/** Populate ListView with list */
ArrayList<String> customerList = new ArrayList<String>();
customerList.addAll( Arrays.asList(rgItems) );
adItemAdapter = new ArrayAdapter<String>(this, R.layout.itemlisttext, rgItems);
lvItemListview.setAdapter(adItemAdapter);
itemlisttext is a xmlLayout with textview
This works fine for me, hope it helps you.
Well you have used counter to count record.
use cursor.getCount();
And use SimpleCursorAdapter
instead of ArrayAdapter
.
It will facilitate you.
Have a look at this Answer
精彩评论