Android recyclerview和sqlite问题

时间:2016-09-01 19:27:31

标签: android sqlite android-recyclerview

我有现有的db文件,我希望用户在db中加载它后,在dbler中搜索单词。但是当向上或向下滚动时有些滞后。所以我想重新设计它,但我不能这样做。当我在onBind中使用AsynTask加载后台时,有时我的行显示为空......

DB文件有三列> ID(整数),数据(文本),类别(文本)

适配器;

public class AdapterSearch extends Adapter<MID, AdapterSearch.Holder> {

    public  AdapterSearch(Context context, List<MID> list) {
        super(context, list);
    }

    public void onBind(final Holder holder, final int pos) {
        final MData   mData     = Database.get(list.get(pos).id());
        if(mData.isValid()) {
            String textData     = mData.getData();
            String textCategory = mData.getCategory();

            holder.txData.setText(textData);
            holder.txCategory.setText(textCategory);
        }
    }

    public Holder createHolder(ViewGroup parent, int viewType) {
        return new Holder(inflater.inflate(R.layout.search_row, parent, false));
    }

    protected class Holder extends RecyclerView.ViewHolder {
        @Nullable @Bind(R.id.data)
        AppCompatTextView txData;
        @Nullable @Bind(R.id.category)
        AppCompatTextView txCategory;

        public Holder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}

MDATA;

public class MData {

    private String
            data,
            category;

    public MData(@Nullable String data, @NonNull String category) {
        this.data         = data;
        this.category          = category;
    }

    public String getCategory() {
        return category;
    }


    public String getData() {
        return data;
    }

}

MID;

public class MID {

    private final int
            id;

    public MID(int id) {
        this.id = id;
    }

    public int id() {return this.id;}

}

数据库;

public class Database extends SQLiteOpenHelper{

    ......

    public static MData get(int mID){
        MData mData = null;
        String   data;
        final Cursor cursor = db.query.....;
        if(cursor != null && cursor.moveToFirst()) {
            mData = new MData(cursor.getString(1), cursor.getString(2));
            cursor.close();
        }

        return mData;
    }

    ......
    ..

}

点击搜索按钮时;

List<MID> searchList = null;
final Cursor cursorMID = db.query......;
if (cursorMID != null && cursorMID.moveToFirst()) {
    searchList = new ArrayList<>();
    do {
        searchList.add(new MID(cursorMID.getInt(0)));
    } while (cursorMID.moveToNext());
    cursorMID.close();
}

rcylerView.setAdapter(getContext(), searchList);

0 个答案:

没有答案