简体   繁体   中英

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.

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.

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

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;
        }
    });

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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