简体   繁体   English

Recyclerview sqllite更新仅适用于列表中的第一项

[英]Recyclerview sqllite update only works on first item on the list

I have a list of items that are displayed in a recylcerview in my adapter class i implmented a onlongclick method, when used it opens up a new alertdialog, when i press on the selected item in the alterdialog im calling updateList() function that is supposed to update the selected item the problem is this only works for the first item displayed in my list, so in exmaple if i pressed the 10th item on the list said item wont update. 我有一个在适配器类的recylcerview中显示的项目列表,我实现了onlongclick方法,当使用它时,它会打开一个新的alertdialog,当我按alterdialog中的所选项目时,我调用了updateList()函数,更新所选项目的问题是,这仅适用于我列表中显示的第一个项目,因此在例中,如果我按列表中的第10个项目,则该项目将不会更新。

Update function 更新功能

public boolean updateList(long rowId, String stanje) {
    ContentValues newValues = new ContentValues();
    newValues.put(TABELA_STOLPEC_STANJE,stanje);
    return db.update(TABELA_IME,newValues,TABELA_STOLPEC_ID+"="+rowId,null)>0;

}

OnlongClickListener this method is in my fragment class. OnlongClickListener这个方法在我的片段类中。

mmAdapter.setOnLongClick(new ToDoRecyclerViewAdapter.OnLongClickListener_() {
            @Override
            public void onLongClick(long i,String item) {
                if(item.equals("doing")) {
                    boolean update_1 = db.updateList(i, item);
                    if(update_1) {                        
                        android.support.v4.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
                        ft.detach(TodoFragment.this).attach(TodoFragment.this).commit();

                        Toast.makeText(getContext(), "Dodano v bazo.!", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(getContext(), "Prislo je do napake!", Toast.LENGTH_SHORT).show();
                    }
                }
            }

        });

onlongclicklistener implementation in my adapter class 我的适配器类中的onlongclicklistener实现

holder.mView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(final View v) {
            AlertDialog.Builder adb = new AlertDialog.Builder(v.getContext());
            CharSequence meni[] = new CharSequence[] {"DOING"};
            adb.setItems(meni, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                if(i == 0) {
                    mValues.get(i).setStanje("doing");
                    clickLinster_.onLongClick(mValues.get(position).getId_(),mValues.get(position).getStanje());
                }
                }
            });
            AlertDialog alertDialog = adb.create();
            alertDialog.setCancelable(true);
            alertDialog.show();
            return true;
        }
    });

   public interface OnLongClickListener_ {
    void onLongClick(long i, String item);
  }

The reason is simple, you are updating wrong row with index, correct solution: 原因很简单,您正在使用索引更新错误的行,正确的解决方案:

@Override
public void onBindViewHolder(@NonNull final CustomViewHolder holder, int position) {

    //Rest of onBindViewHolder code

    holder.mView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(final View v) {
            final int pos = holder.getAdapterPosition();

            AlertDialog.Builder adb = new AlertDialog.Builder(v.getContext());
            CharSequence meni[] = new CharSequence[] {"DOING"};
            adb.setItems(meni, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                if(i == 0) {
                    mValues.get(pos).setStanje("doing");
                    clickLinster_.onLongClick(mValues.get(pos).getId_(),mValues.get(pos).getStanje());
                }
                }
            });
            AlertDialog alertDialog = adb.create();
            alertDialog.setCancelable(true);
            alertDialog.show();
            return true;
        }
    });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM