Android Realm数据库没有更新记录

时间:2017-05-23 11:44:44

标签: android android-recyclerview realm

我是Android开发人员的新手我试图在Recyclerview中对项目位置进行排序我正在使用Realm数据库,而我的适配器数据集是RealmResults<ShoppingList>

这是我的Adapter请查看我的moveItemDownupdateItemPosition方法我试图在updateItemPosition方法中更新项目位置,但它无效。我还将我的视图附在适配器下面

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {

    private RealmResults<ShoppingList> dataList;
    private Realm mRealm;
    private LayoutInflater mInflater;

    public RecyclerAdapter(Context context, RealmResults<ShoppingList> data, Realm reamDatabaseInstence){
        this.dataList = data;
        this.mInflater = LayoutInflater.from(context);
        mRealm = reamDatabaseInstence;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.i("Recycler", "onCreateViewHolder");

        View view = mInflater.inflate(R.layout.shopping_list_item, parent,false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Log.i("Recycler", "onBindViewHolder " + position);

        ShoppingList currentItem = dataList.get(position);
        holder.setData(currentItem, position);
        holder.setListener();
    }

    public void moveItemDown(int position){
        if( position == (dataList.size()-1) ) { return; }
        int oi = position;
        int ni = position+1;
        updateItemPosition(oi, ni);
        notifyDataSetChanged();

    }

    public void moveItemUp(int position){
        // Need to implement after down sort work
    }

    private void updateItemPosition(final int pos, final int new_pos){
        mRealm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                ShoppingList old_item = realm.where(ShoppingList.class)
                        .equalTo("position", pos)
                        .findFirst();
                old_item.setPosition(new_pos);
                Log.d("UpdateData: ",old_item.getTitle()+" "+old_item.getPosition());

                ShoppingList new_item = realm.where(ShoppingList.class)
                        .equalTo("position", new_pos)
                        .findFirst();
                new_item.setPosition(pos);
                Log.d("UpdateData: ",new_item.getTitle()+" "+new_item.getPosition());

            }
        });
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        TextView title;
        ImageButton up, down;
        ImageView image;
        int position;
        ShoppingList current;

        MyViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.shopping_card_view_title);
            image = (ImageView) itemView.findViewById(R.id.shopping_card_view_image);
            up = (ImageButton) itemView.findViewById(R.id.shopping_card_view_up_btn);
            down = (ImageButton) itemView.findViewById(R.id.shopping_card_view_down_btn);
        }

        void setData(ShoppingList i, int position) {
            title.setText( i.getTitle() );
            image.setImageResource(i.getImageId());
            this.position = position;
            this.current = i;
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.shopping_card_view_up_btn:
                    moveItemUp(position);
                    break;
                case R.id.shopping_card_view_down_btn:
                    moveItemDown(position);
                    break;
            }
        }

        void setListener() {
            up.setOnClickListener(MyViewHolder.this);
            down.setOnClickListener(MyViewHolder.this);
        }
    }

}

enter image description here

3 个答案:

答案 0 :(得分:1)

要获得自动更新,您应该使用https://github.com/realm/realm-android-adapters

中的RealmRecyclerViewAdapter
public class RecyclerAdapter extends RealmRecyclerViewAdapter<ShoppingList, RecyclerAdapter.MyViewHolder> { // <-- !
    private Realm mRealm;
    private LayoutInflater mInflater;

    public RecyclerAdapter(Context context, RealmResults<ShoppingList> data, Realm realm){
        super(data, true);
        this.mInflater = LayoutInflater.from(context);
        mRealm = realm;
    }

并将dataList替换为getData()

答案 1 :(得分:1)

它的迟到回复会对您有所帮助,这是两个答案的结合 EpicPandaForce 给出一个很好的例子来与RealmRecyclerViewAdapter合作并制作您的Recyclerview自动化。

第二个选项
这是使用 UNIQUE ID FIELD 以便在列表项中进行更改。

以下是您在代码中发生的问题
基本上当你通过它的位置得到一个项目,例如position=1;并且你更新到位置2对吗?请注意,现在您有两个项目具有相同的position = 2,现在当您尝试使用position = 2获得第一项时,Realm会返回您从第1位更新为2的相同项目现在发生了什么,您更新了日期位置返回到position=1;

taman neupane给你一个好主意,但他没有太多描述。

尝试

private void updateItemPosition(final int pos, final int new_pos){

    final String current_item_id = dataList.get(pos).get_id();
    final String new_item_id = dataList.get(new_pos).get_id();

    mRealm.executeTransactionAsync(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            // Try to find the result with unique id replace this .equalTo("Your Unique ID Field name", current_item_id)
            ShoppingList old_item = realm.where(ShoppingList.class).equalTo("position", pos).findFirst();

            old_item.setPosition(new_pos);
            // Also here Try to find the result with unique id replace this .equalTo("Your Unique ID Field name", new_item_id)
            ShoppingList new_item = realm.where(ShoppingList.class).equalTo("position", new_pos).findFirst();

            new_item.setPosition(pos);
            //new you can tell you adapter about the changes in dataset.
            notifyItemMoved(pos, new_pos);
            notifyItemRangeChanged(pos, dataList.getSize());
        }
    });
}

然后用

更新你的两个函数
public void moveItemDown(int position){
    if( position == (dataList.size()-1) ) { return; }
    int oi = position;
    int ni = position+1;
    updateItemPosition(oi, ni);
}

public void moveItemUp(int position){
    if( position == 0 ) { return; }
    int oi = position;
    int ni = position-1;
    updateItemPosition(ni, oi);
}

那就是它。 如果有人想修改我的解释,我会很感激,因为我的英语不好。

快乐编码。

答案 2 :(得分:0)

我认为您的ShoppingList项目具有所有人的唯一ID。只需将id字段设为primary并使用realmInsertorUpdate功能。您不必检查其他任何东西。如果id可用,它将更新数据库,如果id不可用,它将只插入。