Ncit Cosmos Ncit Cosmos -4 years ago 115
Java Question

Error on Saving data on ArrayList From Multi Select Button Data(Text)

I have been trying it for 2 days, but I can't get the actual solution that how to save in arraylist from multiple button data. There is no problem of retrieving data on button. Just got a problem with saving those button texts on arraylist.
E.g Those buttons consist the category and user chose a multiple category and I have to save that selected data on Array List.
Here's my Code

public class ButtonAdapter extends RecyclerView.Adapter<ButtonAdapter.DerpHolder> {
public List<String> listData;
public Context context;

private LayoutInflater inflater;
public ArrayList<String> dataList;
private List<Integer> selectedItems = new ArrayList<>();

public ButtonAdapter(List<String> listData, Context c) {
this.inflater = LayoutInflater.from(c);
this.listData = listData;
}


public ButtonAdapter(Context applicationContext) {
this.context = applicationContext;
}

@Override
public DerpHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.multiple_button, parent, false);
return new DerpHolder(view);
}

public void add(List<String> interests) {
listData.addAll(interests);
notifyItemRangeInserted(getItemCount(), interests.size());
}

@Override
public void onBindViewHolder(DerpHolder holder, int position) {

String item = listData.get(position);
holder.btnInterest.setText(item);
holder.btnInterest.setSelected(selectedItems.contains(position));


}

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

class DerpHolder extends RecyclerView.ViewHolder {
private Button btnInterest;
private View container;


public DerpHolder(final View itemView) {
super(itemView);
btnInterest = (Button) itemView.findViewById(R.id.btnInterest);
btnInterest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count = listData.size();
String a1[] = new String[count + 1];
dataList = new ArrayList<String>();
int selectedItem = getLayoutPosition();
if (selectedItems.contains(selectedItem)) {
Log.i("Value","Remove: "+selectedItem);
a1[selectedItem] = String.valueOf(getAdapterPosition());
a1[count] = btnInterest.getText().toString();
dataList.remove(a1[count]);

selectedItems.remove((Integer) selectedItem);
} else {
Log.i("Value","Add: "+selectedItem);
a1[selectedItem] = String.valueOf(getAdapterPosition());
a1[count] = btnInterest.getText().toString();
dataList.add(a1[count]);

selectedItems.add((Integer) selectedItem);

}


notifyItemChanged(selectedItem);

Log.i("Value", dataList.size()+"");
}
});

}

}


}

Problem On Code:

btnInterest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count = listData.size();
String a1[] = new String[count + 1];
dataList = new ArrayList<String>();
int selectedItem = getLayoutPosition();
if (selectedItems.contains(selectedItem)) {
Log.i("Value","Remove: "+selectedItem);
a1[selectedItem] = String.valueOf(getAdapterPosition());
a1[count] = btnInterest.getText().toString();
dataList.remove(a1[count]);

selectedItems.remove((Integer) selectedItem);
} else {
Log.i("Value","Add: "+selectedItem);
a1[selectedItem] = String.valueOf(getAdapterPosition());
a1[count] = btnInterest.getText().toString();
dataList.add(a1[count]);

selectedItems.add((Integer) selectedItem);

}


notifyItemChanged(selectedItem);

Log.i("Value", dataList.size()+"");
}
});

Answer Source

Try this:

public class ButtonAdapter extends RecyclerView.Adapter<ButtonAdapter.DerpHolder> {
        public String text = "";
        public List<String> listData;
        public List<Integer> listId;
        public ArrayList<Integer> dataList = new ArrayList<Integer>();
        private LayoutInflater inflater;
        private List<Integer> selectedItems = new ArrayList<>();
        ListItem listInterest = new ListItem();

        public ButtonAdapter(List<String> listData, List<Integer> listId, Context c) {
            this.inflater = LayoutInflater.from(c);
            this.listData = listData;
            this.listId = listId;
        }


        @Override
        public DerpHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = inflater.inflate(R.layout.multiple_button, parent, false);
            return new DerpHolder(view);
        }

        @Override
        public void onBindViewHolder(DerpHolder holder, int position) {
            String item = listData.get(position);
            Integer idSel = listId.get(position);
            holder.btnInterest.setText(item);
            holder.btnInterest.setId(idSel);
            holder.btnInterest.setSelected(selectedItems.contains(position));
        }

        public void add(List<String> interests, List<Integer> selcItem) {
            listData.addAll(interests);
            listId.addAll(selcItem);
            notifyItemRangeInserted(getItemCount(), interests.size());
        }

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

        class DerpHolder extends RecyclerView.ViewHolder {
            private Button btnInterest;
            private View container;

            public DerpHolder(final View itemView) {

                super(itemView);
                btnInterest = (Button) itemView.findViewById(R.id.btnInterest);
                btnInterest.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        int count = listData.size();
                        Integer a1[] = new Integer[count + 1];
                        int selectedItem = getLayoutPosition();
                        if (selectedItems.contains(selectedItem)) {
                           a1[(Integer) selectedItem] = btnInterest.getId();
                            dataList.remove(a1[(Integer) selectedItem]);
                            selectedItems.remove((Integer) selectedItem);
                        } else {
                            a1[(Integer) selectedItem] = btnInterest.getId();
                            dataList.add(a1[(Integer) selectedItem]);
                            selectedItems.add((Integer) selectedItem);
                        }
                        notifyItemChanged(selectedItem);
                        listInterest.setBtnId(dataList);
                    }
                });

            }


        }

        public ArrayList<Integer> getId() {
            ArrayList<Integer> text = listInterest.getBtnId();
            return text;
        }
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download