我有一个列表视图,显示了我的sqlite表。我尝试使用Aray Adapter作为我的第一次体验和所有显示的领域,但它只是凌乱。我想让他们看起来像一个有三列的表。我尝试在this link处回答代码,这是我的代码。
if (Globals.listoff == null){Globals.listoff = dataSource.getAllproductoff();}
adapter = new MySimpleAdapter(ViewDataOff.this,dataSource.getAllproductoff() , R.layout.item_list_offeringpro,
new String[] { "one", "two","three" },
new int[] { R.id.edmodul, R.id.edtype,R.id.edtype, R.id.edwinningpro});
adapter.notifyDataSetChanged();
setListAdapter(adapter);
这是我的DBDataSource.java
public ArrayList<HashMap<String,Object>> getAllproductoff() {
ArrayList<HashMap<String, Object>> listoff = new ArrayList<HashMap<String, Object>>();
Cursor cursor = database.query(DBHelper.TABLE_NAME,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
HashMap<String, Object> map1 = new HashMap<String, Object>();
map1.put("one", cursor.getLong(cursor.getColumnIndex(DBHelper.COLUMN_ID)));
map1.put("two", cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_DESCRIPTION)));
map1.put("three", cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PRICE)));
listoff.add(map1);
cursor.moveToNext();
}
cursor.close();
return listoff;
}
这是MySimpleAdapter.java
public class MySimpleAdapter extends SimpleAdapter {
Context localcontext = null;
public MySimpleAdapter(Context context,ArrayList<HashMap<String, Object>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
localcontext = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
return view;
}
}
这是我的错误日志
02-21 09:48:21.259: E/AndroidRuntime(21895): FATAL EXCEPTION: main
02-21 09:48:21.259: E/AndroidRuntime(21895): java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:160)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
02-21 09:48:21.259: E/AndroidRuntime(21895): at ims.app.mobileorder.MySimpleAdapter.getView(MySimpleAdapter.java:22)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.widget.AbsListView.obtainView(AbsListView.java:2271)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.widget.ListView.makeAndAddView(ListView.java:1769)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.widget.ListView.fillDown(ListView.java:672)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.widget.ListView.fillFromTop(ListView.java:733)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.widget.ListView.layoutChildren(ListView.java:1622)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.widget.AbsListView.onLayout(AbsListView.java:2106)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.view.View.layout(View.java:13932)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.view.ViewGroup.layout(ViewGroup.java:4498)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1693)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1551)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.widget.LinearLayout.onLayout(LinearLayout.java:1456)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.view.View.layout(View.java:13932)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.view.ViewGroup.layout(ViewGroup.java:4498)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.widget.FrameLayout.onLayout(FrameLayout.java:460)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.view.View.layout(View.java:13932)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.view.ViewGroup.layout(ViewGroup.java:4498)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1693)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1551)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.widget.LinearLayout.onLayout(LinearLayout.java:1456)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.view.View.layout(View.java:13932)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.view.ViewGroup.layout(ViewGroup.java:4498)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.widget.FrameLayout.onLayout(FrameLayout.java:460)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.view.View.layout(View.java:13932)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.view.ViewGroup.layout(ViewGroup.java:4498)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1874)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1695)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1002)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4290)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.view.Choreographer.doFrame(Choreographer.java:525)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.os.Handler.handleCallback(Handler.java:615)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.os.Handler.dispatchMessage(Handler.java:92)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.os.Looper.loop(Looper.java:137)
02-21 09:48:21.259: E/AndroidRuntime(21895): at android.app.ActivityThread.main(ActivityThread.java:4803)
02-21 09:48:21.259: E/AndroidRuntime(21895): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 09:48:21.259: E/AndroidRuntime(21895): at java.lang.reflect.Method.invoke(Method.java:511)
02-21 09:48:21.259: E/AndroidRuntime(21895): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
02-21 09:48:21.259: E/AndroidRuntime(21895): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
02-21 09:48:21.259: E/AndroidRuntime(21895): at dalvik.system.NativeStart.main(Native Method)
感谢您的帮助..所有人都将不胜感激。
答案 0 :(得分:4)
更改cursor.moveToNext();
以使您的代码类似
if (cursor.moveToFirst()) {
do {
HashMap<String, Object> map1 = new HashMap<String, Object>();
map1.put("one", cursor.getLong(cursor.getColumnIndex(DBHelper.COLUMN_ID)));
map1.put("two", cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_DESCRIPTION)));
map1.put("three", cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PRICE)));
listoff.add(map1);
} while (cursor.moveToNext());
}
答案 1 :(得分:2)
在你做之外定义cursor.moveToNext();
并在使用前始终检查getCount
。
if(cursor!=null && cursor.getCount()>0){
cursor.moveToNext();
do {
HashMap<String, Object> map1 = new HashMap<String, Object>();
map1.put("one", cursor.getLong(cursor.getColumnIndex(DBHelper.COLUMN_ID)));
map1.put("two", cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_DESCRIPTION)));
map1.put("three", cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PRICE)));
listoff.add(map1);
} while (cursor.moveToNext());
}