簡體   English   中英

Recyclerview sqllite更新僅適用於列表中的第一項

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

我有一個在適配器類的recylcerview中顯示的項目列表,我實現了onlongclick方法,當使用它時,它會打開一個新的alertdialog,當我按alterdialog中的所選項目時,我調用了updateList()函數,更新所選項目的問題是,這僅適用於我列表中顯示的第一個項目,因此在例中,如果我按列表中的第10個項目,則該項目將不會更新。

更新功能

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這個方法在我的片段類中。

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實現

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

原因很簡單,您正在使用索引更新錯誤的行,正確的解決方案:

@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