0

这是我的自定义适配器:

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

这是我的 Search Edittext aftertextchanged 事件

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;

                }
            });

最后:Logcat 异常

04-26 18:56:50.310: E/AndroidRuntime(878): 致命异常: main 04-26 18:56:50.310: E/AndroidRuntime(878): android.database.CursorIndexOutOfBoundsException: 请求索引-1,大小1 04-26 18:56:50.310: E/AndroidRuntime(878): 在 android.database.AbstractCursor.checkPosition(AbstractCursor.java:580) 04-26 18:56:50.310: E/AndroidRuntime(878): 在android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214) 04-26 18:56:50.310: E/AndroidRuntime(878): 在 android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41) 04-26 18: 56:50.310: E/AndroidRuntime(878): 在 master.chef.mediamaster.Interface.onItemClick(Interface.java:781) 04-26 18:56:50.310: E/AndroidRuntime(878): 在 android.widget.AdapterView .performItemClick(AdapterView.java:284) 04-26 18:56:50.310: E/AndroidRuntime(878):在 android.widget.ListView.performItemClick(ListView.java:3513) 04-26 18:56:50.310: E/AndroidRuntime(878): 在 android.widget.AbsListView$PerformClick.run(AbsListView.java:1812) 04- 26 18:56:50.310: E/AndroidRuntime(878): 在 android.os.Handler.handleCallback(Handler.java:587) 04-26 18:56:50.310: E/AndroidRuntime(878): 在 android.os。 Handler.dispatchMessage(Handler.java:92) 04-26 18:56:50.310: E/AndroidRuntime(878): 在 android.os.Looper.loop(Looper.java:123) 04-26 18:56:50.310: E/AndroidRuntime(878): 在 android.app.ActivityThread.main(ActivityThread.java:3683) 04-26 18:56:50.310: E/AndroidRuntime(878): 在 java.lang.reflect.Method.invokeNative(Native方法) 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) : 在 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):在 dalvik.system.NativeStart.main(本机方法)

4

1 回答 1

1

getView()你打电话时,localCursor.moveToPosition(position);你会得到java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery.

为什么会发生这种情况?因为一旦对列表视图应用了过滤,旧的光标就会关闭,而使用新的光标。getCursor()您可以通过调用而不是跟踪初始光标来获取当前的底层光标。摆脱Cursor localCursor并使用getCursor()

于 2012-04-22T07:49:54.213 回答