OnClick从RecyclerView删除项目?

时间:2018-12-01 23:06:15

标签: android android-recyclerview

我有一个RecyclerView项目XML,当创建每个项目时,该XML有两个TextViews要填写。然后将它们显示在垂直的RecyclerView中。我的意图是添加一个OnLongClick操作以显示一个对话框,以确认用户是否要删除此项目。如果确认,我想立即从显示屏中删除该项目以及适配器中存储的数据。

代码:

package uk.ac.aber.dcs.haa14.assignment.classes;

public class DictionaryRecyclerViewAdapter extends RecyclerView.Adapter<DictionaryRecyclerViewAdapter.DictionaryViewHolder> {

public List<String> primaryWords;
public List<String> secondaryWords;
Context context;
int size;

public DictionaryRecyclerViewAdapter(LinkedHashMap<String,String> wordPairs, Context inContext){
    primaryWords = new ArrayList<>(wordPairs.keySet());
    secondaryWords = new ArrayList<>(wordPairs.values());
    this.size = wordPairs.size();
    this.context = inContext;
}

@NonNull
@Override
public DictionaryViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.dictionary_words_recycler_item,viewGroup,false);
    return new DictionaryViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull DictionaryViewHolder dictionaryViewHolder, int i) {
    dictionaryViewHolder.bind(primaryWords.get(i), secondaryWords.get(i));
}

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

class DictionaryViewHolder extends RecyclerView.ViewHolder {

    TextView leftWord;
    TextView rightWord;

    public DictionaryViewHolder(View itemView) {
        super(itemView);
        final View tempView = itemView;
        leftWord = itemView.findViewById(R.id.dictRecyclerViewLeftWord);
        rightWord = itemView.findViewById(R.id.dictRecyclerViewRightWord);
    }

    public void bind(String primWord, String secWord){
        leftWord.setText(primWord);
        rightWord.setText(secWord);

        itemView.setOnLongClickListener(new View.OnLongClickListener(){
            @Override
            public boolean onLongClick(View view) {
                DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch(which){
                            case DialogInterface.BUTTON_POSITIVE:
                                int position = getAdapterPosition();
                                primaryWords.remove(position);
                                secondaryWords.remove(position);
                                notifyItemRangeChanged(position, primaryWords.size());
                                //TODO - Call Globals for remove word from lists
                                break;
                            case DialogInterface.BUTTON_NEGATIVE:
                                break;

                        }
                    }
                };
                AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                builder.setMessage("Would you like to delete this word pair?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();
                return true;
            }
        });
    }
}
}

最终,我希望用户通过“浮动操作按钮”添加新对,但一次只能做一件事。目前,为RecyclerView创建了五个项目:

Android Screenshot

每行/每对都是RecyclerView中的单个项目

错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: uk.ac.aber.dcs.haa14.assignment, PID: 26412
java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
    at java.util.ArrayList.get(ArrayList.java:437)
    at uk.ac.aber.dcs.haa14.assignment.classes.DictionaryRecyclerViewAdapter.onBindViewHolder(DictionaryRecyclerViewAdapter.java:44)
    at uk.ac.aber.dcs.haa14.assignment.classes.DictionaryRecyclerViewAdapter.onBindViewHolder(DictionaryRecyclerViewAdapter.java:21)
    at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
    at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
...

2 个答案:

答案 0 :(得分:1)

我有一个类似的问题,可以通过以下方法解决:

// Add 2 method 
notifyDataSetChanged()
notifyItemRemoved(position)

答案 1 :(得分:1)

您的错误是getItemCount()总是返回size,其值不会改变。只需将其更改为返回primaryWords.size()(并删除size字段),就可以了。