Mohamed Hemdan
Mohamed Hemdan

Reputation: 303

Search the ListView

Here is my Custom Adapter :

public View getView(int position, View convertView, ViewGroup parent) {

    View row = super.getView(position, convertView, parent);


    Cursor cursbbn  = getCursor();



    if (row == null)

    {

        LayoutInflater inflater = (LayoutInflater) localContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.listtype, null);
    }


    String Title = cursbbn.getString(2);
    String Readyin = cursbbn.getString(4);
    String Faovoites=cursbbn.getString(8);


    TextView titler=(TextView)row.findViewById(R.id.listmaintitle);
    TextView readyinr=(TextView)row.findViewById(R.id.listreadyin);

     int colorPos = position % colors.length;
     row.setBackgroundColor(colors[colorPos]);

    titler.setText(Title);
    readyinr.setText(Readyin);

    ImageView picture = (ImageView) row.findViewById(R.id.imageView1);
    Bitmap bitImg = BitmapFactory.decodeResource(localContext.getResources(), R.drawable.seafood);

    if(Title.contentEquals("Fajita Raps"))
        picture.setImageBitmap(getRoundedCornerImage(bitImg));


    if (Faovoites.contentEquals("YES")) {

        ImageView star = (ImageView) row.findViewById(R.id.favoritesicon);
        star.setVisibility(View.VISIBLE);

    }

    return row;
}

And This is my Search Edittext aftertextchanged event

public void afterTextChanged(final Editable s) {

            final String[] columns = new String[] {

            "_id", COL_SanID, COL_SanTitle, COL_SanCat, COL_SanReadyin,

            COL_SandServing, COL_SandIngred, COL_SandDirect, COL_SandFavor };

            Bundle extrass = getIntent().getExtras();

            final String Type = extrass.getString("CategoryType");

            mAdapter.setFilterQueryProvider(new FilterQueryProvider() {
                public Cursor runQuery(CharSequence constraint) {

                    String value = "%" + constraint + "%";
                    String Type2 = "%" + Type + "%";

                    curs = mDb.query(TABLE_NAME, columns, COL_SanTitle
                            + " LIKE ? And " + COL_SanCat + " LIKE ?",
                            new String[] { value, Type2 }, null, null,
                            COL_SanTitle + " ASC");

                    return curs;

                }
            });

and Finally : Logcat exceptions

04-26 18:56:50.310: E/AndroidRuntime(878): FATAL EXCEPTION: main 04-26 18:56:50.310: E/AndroidRuntime(878): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 04-26 18:56:50.310: E/AndroidRuntime(878): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580) 04-26 18:56:50.310: E/AndroidRuntime(878): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214) 04-26 18:56:50.310: E/AndroidRuntime(878): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41) 04-26 18:56:50.310: E/AndroidRuntime(878): at master.chef.mediamaster.Interface.onItemClick(Interface.java:781) 04-26 18:56:50.310: E/AndroidRuntime(878): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 04-26 18:56:50.310: E/AndroidRuntime(878): at android.widget.ListView.performItemClick(ListView.java:3513) 04-26 18:56:50.310: E/AndroidRuntime(878): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812) 04-26 18:56:50.310: E/AndroidRuntime(878): at android.os.Handler.handleCallback(Handler.java:587) 04-26 18:56:50.310: E/AndroidRuntime(878): at android.os.Handler.dispatchMessage(Handler.java:92) 04-26 18:56:50.310: E/AndroidRuntime(878): at android.os.Looper.loop(Looper.java:123) 04-26 18:56:50.310: E/AndroidRuntime(878): at android.app.ActivityThread.main(ActivityThread.java:3683) 04-26 18:56:50.310: E/AndroidRuntime(878): at java.lang.reflect.Method.invokeNative(Native Method) 04-26 18:56:50.310: E/AndroidRuntime(878): at java.lang.reflect.Method.invoke(Method.java:507) 04-26 18:56:50.310: E/AndroidRuntime(878): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 04-26 18:56:50.310: E/AndroidRuntime(878): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 04-26 18:56:50.310: E/AndroidRuntime(878): at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 522

Answers (1)

Vit Khudenko
Vit Khudenko

Reputation: 28418

In getView() when you call localCursor.moveToPosition(position); you get java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery.

Why this happens? Because once filtering has been applied to the list view, the old cursor is closed and the new one is used instead. You can get the current underlying cursor by calling getCursor() instead of tracking the initial one. Get rid of Cursor localCursor and use getCursor() instead.

Upvotes: 1

Related Questions