对筛选的sqlite查询进行排序

时间:2013-04-29 04:45:23

标签: android sorting filter android-sqlite

所以我有一段代码从一个充满用户条目的数据库中检索数据。我还有一个带文本观察器的EditText,允许他们通过输入过滤条目。最后,我有一个微调器,允许用户对返回的查询进行排序。

除非用户过滤条目然后尝试对其进行排序,否则一切正常。此时,所有条目都被重新加载和排序。

所以显然过滤器没有被重新接合,但是我没有理解为什么或要做什么来修复它。

private void displayListView(String sortdata) {
    // TODO Auto-generated method stub
    // Building the list
    info.open();
    Cursor cursor = info.getListData(sortdata);
    info.close();
    String[] listViewColumns = new String[] { Database.KEY_BREWNAME, Database.KEY_BREWERY, Database.KEY_RATING,
            Database.KEY_DATEIMAGENAME, };
    int[] dataToListView = new int[] { R.id.tvBrewName, R.id.tvBrewery, R.id.tvRating, R.id.ivThumb, };
    dataAdapter = new SimpleCursorAdapter(this, R.layout.entrymain, cursor, listViewColumns, dataToListView, 0);
    ViewBinder viewBinder = new ViewBinder() {
        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            if (!(view instanceof ImageView) || view.getId() != R.id.ivThumb) {
                return false;
            }
            ImageView image = (ImageView)view;

            if (new File(Environment.getDataDirectory() + "/data/com.ex.beerlog/files/" + cursor.getString(columnIndex))
                    .exists()) {
                try {
                    FileInputStream fis = openFileInput(cursor.getString(columnIndex));
                    Bitmap bmp1 = BitmapFactory.decodeStream(fis);
                    bmp1 = cropToCicle(bmp1);
                    image.setImageBitmap(bmp1);
                    fis.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                image.setImageResource(R.drawable.bl);
                Log.w("Start", "=-=-=-=-" + cursor.getString(columnIndex) + " === Doesn't exist");
            }
            return true;
        }
    };
    dataAdapter.setViewBinder(viewBinder);
    listview.setAdapter(dataAdapter);

    // What happens when a list item is clicked
    OnItemClickListener onListItemClick = new OnItemClickListener() {
        public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
            Cursor clickCursor = (Cursor)listView.getItemAtPosition(position);
            String clickData = clickCursor.getString(clickCursor.getColumnIndexOrThrow("_id"));
            Bundle backpack = (Bundle)new Bundle();
            backpack.putString("clickdata", clickData); 
            Intent a = new Intent(Start.this, AddEdit.class);
            a.putExtras(backpack);
            startActivity(a);
        }
    };
    listview.setOnItemClickListener(onListItemClick);

    // /Modifying the list entries while the user types, setup a filter
    TextWatcher watchSearch = new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {}

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            dataAdapter.getFilter().filter(s.toString());
        }
    };
    sqlSearchText.addTextChangedListener(watchSearch);

    FilterQueryProvider filterQuery = new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            info.open();
            return info.fetchEntriesByName(constraint.toString(), sortSpinValues);
        }
    };
    info.close();
    dataAdapter.setFilterQueryProvider(filterQuery);

}


public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long arg3) {
    // TODO Auto-generated method stub
    sortSpinValues = sortSpinValue[pos];
    displayListView(sortSpinValues);
}

我认为这可能是非常简单的事情,我似乎无法弄明白。

由于

1 个答案:

答案 0 :(得分:0)

因此花了一点时间了解过滤器的工作方式,并意识到在微调器值更改后我没有重新加载过滤器(或者我不知道什么没有被重新加载)。所以我可以使用这些额外的代码来解决问题。

if (sqlSearchText.length() > 1) {
            String f = sqlSearchText.getText().toString();
            dataAdapter.getFilter().filter(f);
        }