Java – Data cannot be viewed using the simple adapter Sqlite

Data cannot be viewed using the simple adapter Sqlite… here is a solution to the problem.

Data cannot be viewed using the simple adapter Sqlite

I have a ListView that shows my sqlite table. I tried using Aray Adapter as my first experience and all the fields were shown, but it was just a mess. I want them to look like a three-column table. I tried to implement the code from the answer to this link, here is my code.

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); 

This is my 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;
}

This is the 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;
}

Here is my error log

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)

Thanks for your help. We would appreciate it.

Solution

change cursor.moveToNext(); Make your code look like this

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());
}

Related Problems and Solutions