返回的ArrayList大小为0

时间:2018-02-15 13:27:07

标签: java android arraylist android-adapter indexoutofboundsexception

我的适配器类中有一个arraylist。我在OnLongClick()方法中添加了一些项目。当我使用Log内部OnLongClick()方法打印它的大小时,它会在添加一个项目时打印大小1但是如果我在另一个方法getList()中打印它,则在添加一个项目时打印大小为0。

适配器:

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {

    private ArrayList<String> mData;
    private ArrayList<String> mData2;
    private LayoutInflater mInflater;
    private int selected_position ;
    private ItemClickListener mClickListener;
    private ArrayList<String> mSelected = new ArrayList<>();
    ArrayList<Uri> files = new ArrayList<>();
    private static final String TAG = "com.example.dell_1.myapp3.InternalMemory";
    private Context context;

    // data is passed into the constructor
    public MyRecyclerViewAdapter(Context context, ArrayList<String> data, ArrayList<String> data2) {
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
        this.mData2 = data2;
        this.context = context;
    }

    @Override
        public boolean onLongClick(View view) {  //this is the method asked by user902383
            if (mClickListener != null) mClickListener.onLongClick(view, getAdapterPosition());
            selected_position = getAdapterPosition();
            if (mSelected.contains(Integer.toString(selected_position))) {
                mSelected.remove(Integer.toString(selected_position));
                itemView.setBackgroundColor(Color.TRANSPARENT);// remove item from list;
                // update view (v) state here
                // eg: remove highlight
            } else {
                mSelected.add(Integer.toString(selected_position));
                itemView.setBackgroundColor(Color.LTGRAY);
                // add item to list
                // update view (v) state here
                // eg: add highlight
            }
            Log.v(TAG, Integer.toString(mSelected.size()) + " this is size");  //this returns size 1
            return  true;
        }
    }

   protected  ArrayList<String>  getList(){
            Log.v(TAG,Integer.toString(mSelected.size()) + "  final size"); //this returns size 0
            return mSelected;

        }

日志:

02-16 16:00:52.129 16797-16797/com.example.dell_1.Myapp3 V/com.example.dell_1.myapp3.InternalMemory: 0  final size
02-16 16:00:52.130 16797-16797/com.example.dell_1.Myapp3 V/com.example.dell_1.myapp3.InternalMemory: 1 this is size

此外,很多人都说我在添加项目后删除了该项目,但selected_position = getAdapterPosition(); 表示其值不同,因此请点击不同的项目。我写了这个方法来在选择中添加项目并在取消选择时删除,所以我在取消选择时删除已经添加的项目(再次长按已经选中的项目)。

1 个答案:

答案 0 :(得分:0)

这是因为你要从列表中删除你在onLongClick()上的每个项目中的项目:

@Override
public boolean onLongClick(View view) { 

  if (mSelected.contains(Integer.toString(selected_position))) {
     mSelected.remove(Integer.toString(selected_position));
     ...
  } else {
     ...
  }
  ...
}

您可以通过以下方式删除项目视图,而不是从列表中删除项目:

itemView.setVisibility(View.GONE); 
itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));

阅读更多https://stackoverflow.com/a/46342024/4758255

的设定视图