Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

somebody pls get me out of this.I am trying to display a list from an sqlite database which worked absolutely fine but dont know what went wrong it showed cant find provider info.I fixed it and then when i am running the code with list_cursor.moveToFirst() it just shows the 1st item in list again and again which proves that it is fetching data....When I use list_cursor.moveToNext() it shows the following exception:PLS HELP ME

10-13 15:11:41.017 5337-5337/com.phase3.mascotnew E/AndroidRuntime: FATAL EXCEPTION: main
                                                                Process: com.phase3.mascotnew, PID: 5337
                                                                android.database.CursorIndexOutOfBoundsException: Index 3 requested, with a size of 3
                                                                    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
                                                                    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:147)
                                                                    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:61)
                                                                    at android.database.CursorWrapper.getString(CursorWrapper.java:114)
                                                                    at com.phase3.mascotnew.SelectRecipeActivity$MyCursorAdapter.getView(SelectRecipeActivity.java:145)
                                                                    at android.widget.AbsListView.obtainView(AbsListView.java:2338)
                                                                    at android.widget.ListView.makeAndAddView(ListView.java:1813)
                                                                    at android.widget.ListView.fillDown(ListView.java:698)
                                                                    at android.widget.ListView.fillFromTop(ListView.java:759)
                                                                    at android.widget.ListView.layoutChildren(ListView.java:1646)
                                                                    at android.widget.AbsListView.onLayout(AbsListView.java:2149)
                                                                    at android.view.View.layout(View.java:15140)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:4867)
                                                                    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1160)
                                                                    at android.view.View.layout(View.java:15140)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:4867)
                                                                    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
                                                                    at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
                                                                    at android.view.View.layout(View.java:15140)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:4867)
                                                                    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
                                                                    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
                                                                    at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
                                                                    at android.view.View.layout(View.java:15140)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:4867)
                                                                    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
                                                                    at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
                                                                    at android.view.View.layout(View.java:15140)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:4867)
                                                                    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2474)
                                                                    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2180)
                                                                    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
                                                                    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6412)
                                                                    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
                                                                    at android.view.Choreographer.doCallbacks(Choreographer.java:591)
                                                                    at android.view.Choreographer.doFrame(Choreographer.java:560)
                                                                    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
                                                                    at android.os.Handler.handleCallback(Handler.java:808)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:103)
                                                                    at android.os.Looper.loop(Looper.java:193)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5299)
                                                                    at java.lang.reflect.Method.invokeNative(Native Method)
                                                                    at java.lang.reflect.Method.invoke(Method.java:515)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
                                                                    at dalvik.system.NativeStart.main(Native Method)

SelectRecipeActivity.java

 package com.phase3.mascotnew;

 import android.annotation.TargetApi;
 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.database.Cursor;
 import android.os.Bundle;
 import android.os.Environment;
 import android.support.v7.app.AppCompatActivity;
 import android.util.Log;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.AdapterView;
 import android.widget.ListView;
 import android.widget.SimpleCursorAdapter;
 import android.widget.TextView;

 import com.phase3.mascotnew.database.Tables.Recipe;

 import java.io.File;

 public class SelectRecipeActivity extends Activity {

 private ListView mListView = null;
 private Cursor mCursor = null;
 private SimpleCursorAdapter adapter;

private static final String TAG = "SELECT RECIPE ACTIVITY";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_recipe2);
        mListView = (ListView) findViewById(R.id.recipe_list);

        createRecipeSubFolder();
        showTableItems();
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             Cursor cursor=(Cursor)mListView.getAdapter().getItem(position);
                Log.i(TAG, "Fetching item position" + position);
                String pass_recname = cursor.getString(cursor.getColumnIndex(Recipe.NAME_NIC));
             int pass_groupid = (cursor.getInt(cursor.getColumnIndex(Recipe.GROUPID_NIC)));
                Log.i(TAG, "Fetching data" + " " + pass_recname + " " + pass_groupid);
                cursor.close();

                    Intent i = new Intent(SelectRecipeActivity.this, WhatYouNeedActivity.class);
                    i.putExtra("Title", pass_recname);
                i.putExtra("GroupId", pass_groupid);
                startActivity(i);
                Log.i(TAG, "Sending Data");


                                         }

        });
    }

@Override
protected void onResume() {
    super.onResume();

    showTableItems();
}

@Override
protected void onPause() {
    super.onPause();

    if(mCursor != null){
        mCursor.close();
        mCursor = null;
    }
}
public void showTableItems() {
    if (mCursor != null) {
        mCursor.close();
        mCursor = null;
    }
    int StatusVal=1;
    String Status=Recipe.STATUS+"="+ StatusVal;
    //String selection=Recipe.GROUPID;
    //String selection = Recipe.GROUPID + " "+ "AND"+" "+Status;
    mCursor = getContentResolver().query(Recipe.CONTENT_URI, Recipe.PROJECTION_ALL,Status, null, null);
   if (mCursor == null || mCursor.getCount() == 0) {
        notifyDataExist(false);
        return;
    }
    notifyDataExist(true);

        String[] from = {Recipe.NAME_NIC};
        int[] to = {R.id.recname_row};
        Log.i(TAG, "Setting Adapter");
        adapter = new MyCursorAdapter(this, R.layout.list_recipes_row, mCursor, from, to);
        mListView.setAdapter(adapter);
}


private void notifyDataExist(boolean exist)
{
    if(exist)
    {
        mListView.setVisibility(View.VISIBLE);
    }
    else
    {
        Log.i(TAG, "Recipe Table has no data");
        mListView.setVisibility(View.GONE);
    }
}

class MyCursorAdapter extends SimpleCursorAdapter {
    Activity mActivity = null;
    int mLayoutId;
    String[] from = null;
    int[] to = null;

    public MyCursorAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
        super(context, layout, cursor, from, to, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        mActivity = (Activity) context;
        mLayoutId = layout;
        this.from = from;
        this.to = to;
    }
    @Override
    public int getCount() {
        return mCursor.getCount() + 1;
    }

    @Override
    @TargetApi(15)
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            row = mActivity.getLayoutInflater().inflate(mLayoutId, null, false);
        }
            Cursor list_cursor = getCursor();
            list_cursor.moveToPosition(position);
            list_cursor.moveToNext();
            TextView txtSecondCell = (TextView) row.findViewById(R.id.recname_row);
            String recipename = list_cursor.getString(list_cursor.getColumnIndex(Recipe.NAME_NIC));
            txtSecondCell.setText(recipename);
            Log.i(TAG, "Showing List");
        return row;
    }
}

private void createRecipeSubFolder(){
    Cursor cursor1 = getContentResolver().query(Recipe.CONTENT_URI, Recipe.PROJECTION_ALL, null, null, null);
    while(cursor1.moveToNext()){
//                String path = Environment.getExternalStorageDirectory()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
395 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try this solution

 @Override
    @TargetApi(15)
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            row = mActivity.getLayoutInflater().inflate(mLayoutId, null, false);
        }
            Cursor list_cursor = getCursor();
            list_cursor.moveToPosition(position);

            if(!list_cursor.isLast()){

                 list_cursor.moveToNext();
                 TextView txtSecondCell = (TextView) row.findViewById(R.id.recname_row);
                 String recipename = list_cursor.getString(list_cursor.getColumnIndex(Recipe.NAME_NIC));

                 txtSecondCell.setText(recipename);
                 Log.i(TAG, "Showing List");
            }
        return row;
   }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...