vendredi 19 juin 2015

SQLite cursor puts data in wrong columns

I've implemented filtering in my ListView. But cursor puts data in wrong columns.

E.g.: Cursor binds value from ID column to NAME column etc. How can I resolve it?

Adapter class

public class MyAdapter extends CursorAdapter implements Filterable{

private final String LOG_TAG = MyAdapter.class.getSimpleName();

public MyAdapter(Context context, Cursor c, int flags) {
    super(context, c, flags);
}

public static class ViewHolder {
    public final TextView nameView;
    public final TextView descrView;

    public ViewHolder(View view) {
        nameView = (TextView) view.findViewById(R.id.list_item_textview_name);
        descrView = (TextView) view.findViewById(R.id.list_item_textview_description);
    }
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View view =  LayoutInflater.from(context).inflate(R.layout.list_item_it, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    view.setTag(viewHolder);
    return view;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder viewHolder = (ViewHolder) view.getTag();
    String name = cursor.getString(FruitsFragment.COL_NAME);
    String description = cursor.getString(FruitsFragment.COL_DESCRIPTION);
    viewHolder.nameView.setText(name);
    viewHolder.descrView.setText(description);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return super.getView(position, convertView, parent);
}

}

Filtering

ListView listView = (ListView)rootView.findViewById(R.id.listview_rd);
    mRecipeAdapter = new RecipeAdapter(getActivity(), null, 0);
    mRecipeAdapter.setFilterQueryProvider(new FilterQueryProvider() {
        @Override
        public Cursor runQuery(CharSequence constraint) {
            String queryStr = "%" + constraint.toString() + "%";
            return getAllBy("red", queryStr);
        }
    });

    searchView = (SearchView)getActivity().findViewById(R.id.searchView);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            mRecipeAdapter.getFilter().filter(query);
            mRecipeAdapter.notifyDataSetChanged();
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });
    listView.setTextFilterEnabled(true);
    listView.setAdapter(mRecipeAdapter);

and query method

public Cursor getAllBy(String type, String name) {
    SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    if (db == null) {
        return null;
    }
    String[] args = {type, name};
    return db.rawQuery("select * from records where type = ? and name like ?", args);
}

Aucun commentaire:

Enregistrer un commentaire