内容提供者的query()方法在回收器视图适配器中两次返回数据

时间:2018-06-30 12:21:24

标签: java android sqlite

我正在从事一个android项目,在这里我有一个CategoryProvider类,它扩展了ContentProvider类,并且在我的主要Activity中也实现了LoaderManager.LoaderCallbacks<Cursor>,我也有{{1} }扩展CategoryAdapter的类。我的RecyclerView.Adapter<CategoryAdapter.ViewHolder>数据库中有3条记录,现在在主Activity中的Loader初始化之后,当数据正在适配器中加载时,它会将每条记录加载两次,以便解决该问题。

  

CategotyAdapter.java

Sqlite
  

MainActivity.java

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {
    Cursor dataCursor;
    Context context;
    String global_lang = "English";

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView name;
        ImageView imageView;

        public ViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.titleText);
            imageView = (ImageView) itemView.findViewById(R.id.icons);
        }
    }

    public CategoryAdapter(Activity mContext, Cursor cursor) {
        dataCursor = cursor;
        context = mContext;
    }

    @Override
    public CategoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View cardview = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.option_layout, parent, false);
        return new ViewHolder(cardview);
    }

    @Override
    public void onBindViewHolder(CategoryAdapter.ViewHolder holder, int position) {

        dataCursor.moveToPosition(position);

        String imagePath = dataCursor.getString(dataCursor.getColumnIndex(CategoryContract.CategoryEntry.COLUMN_IMAGE_PATH));
        String cEn_name = dataCursor.getString(dataCursor.getColumnIndex(CategoryContract.CategoryEntry.COLUMN_CONTENT_EN));
        String cHi_name = dataCursor.getString(dataCursor.getColumnIndex(CategoryContract.CategoryEntry.COLUMN_CONTENT_HI));

        if(global_lang.equals("English"))
            holder.name.setText(cEn_name);
        else
            holder.name.setText(cHi_name);

        Glide.with(context)
                .load(imagePath)
                .apply(new RequestOptions()
                .placeholder(R.drawable.loading))
                .into(holder.imageView);

    }

    public Cursor swapCursor(Cursor cursor) {
        if(dataCursor == cursor) {
            return null;
        }
        Cursor oldCursor = dataCursor;
        this.dataCursor = cursor;
        if(cursor != null) {
            this.notifyDataSetChanged();
        }
        return oldCursor;
    }

    @Override
    public int getItemCount() {
        if(dataCursor != null)
            Log.d("datacursor count", " " +dataCursor.getCount());
        else
            Log.d("datacursor count", "datacursor is null");

        return (dataCursor == null) ? 0 : dataCursor.getCount();
    }

    public void changeLang(String lang) {
        this.global_lang = lang;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

好吧,经过大量调试,我明白了。在这里,我使用的是我的App中的“同步适配器”框架,该框架在后台同步数据,当它尝试重新加载数据时,重复的数据会与新ID快速同步,并由recyclerView呈现。所以现在我在使用一些约束并更改数据库结构。