Search code examples
androidjsonandroid-recyclerviewandroid-edittextadapter

RecyclerViewAdapter removed fill content on scroll


i use RecyclerViewAdapter for my json adaper.i make some radiobutton and check box and edittext in RecyclerViewAdapter programically but when i check some radio button or type in edittext and scroll more than 1 screen when i come back all my fill content are removed;

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

    public MyRecyclerViewAdapter(Context context, ArrayList<HashMap<String, Object>> hash_all) {

    }

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

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        hash_all.size();

       hash_get = hash_all.get(position);



        LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(
                LinearLayoutCompat.LayoutParams.MATCH_PARENT,
                LinearLayoutCompat.LayoutParams.WRAP_CONTENT
        );
        params.setMargins(0, 0, 0, 0);

        String s = hash_get.get("ModelShowOption").toString();
        String toggle = "";
        String direc = "";
        String type = "";
        String model = "";
        String rows = "";
        String resize = "";
        String[] split = s.split("\\|");

        String[] split2 = new String[0];
        for (int i = 0; i < split.length; i++) {
            String s2 = split[i];
            split2 = s2.split(":");
            if (split2[0].equals("isToggleButton")){
                toggle=split2[1];
            }else if (split2[0].equals("type")){
                type=split2[1];
            }else if (split2[0].equals("direction")){
                direc=split2[1];
            }else if (split2[0].equals("model")){
                model=split2[1];
            }else if (split2[0].equals("rows")){
                rows=split2[1];
            }else if (split2[0].equals("resize")){
                resize=split2[1];
            }

        }

        String title = hash_get.get("title").toString();

        String myValue =hash_get.get("MyValue").toString();
        String[] splitValue = myValue.split("\\|");

        RadioGroup[] rg;
        RadioButton[] rb;

        if (toggle.equals("true") && holder.row_list_title.getText().equals("")){
            holder.row_list_placeholder.setVisibility(View.GONE);
            holder.row_list_buttn.setVisibility(View.GONE);

            String multiValue = hash_get.get("MultiValue").toString();
            String[] splitMultiValue = multiValue.split(":");
            if (splitMultiValue[1].equals("1")){

            int p = 0;
            rg = new RadioGroup[1];
            rb = new RadioButton[splitValue.length];
            rg[0] = new RadioGroup(holder.row_list_placeholder.getContext());
            rg[0].setOrientation(RadioGroup.VERTICAL);
            for (int j = 0; j < splitValue.length; j++) {

                    String myValue2 = splitValue[j];
                    String[] splitValue2 = myValue2.split("=");
                    rb[j] = new RadioButton(getApplicationContext());
                    rg[p].addView(rb[j]);
                    rb[j].setId(j);
                    rb[j].setText(splitValue2[0]);

            }

            holder.linearLayoutCheckBox.addView(rg[p]);

            p++;

            holder.row_list_title.setText(title);

            if (rg[0].getCheckedRadioButtonId() == -1) {

            } else {
                for (int r = 0; r < splitValue.length; r++) {
                    if (rb[r].isChecked()) {
                        RadioButton id = (RadioButton) findViewById(rb[r].getId());
                        String radioText = id.getText().toString();

                    }
                }
            }

            }else{

                holder.row_list_title.setText(title);

                CheckBox[] ch;
                final int[] checkd = {0};
                List<String> checkvl = new ArrayList<>();
                ch = new CheckBox[splitValue.length];

                    for (int j = 0; j < splitValue.length; j++) {


                        String myValue2 = splitValue[j];
                        String[] splitValue2 = myValue2.split("=");
                        ch[j] = new CheckBox(getApplicationContext());
                        holder.linearLayoutCheckBox.addView(ch[j]);
                        ch[j].setId(j);
                        ch[j].setText(splitValue2[0]);

                    }


                for (int r = 0; r < splitValue.length; r++) {
                    int finalR = r;
                    int finalR1 = r;
                    ch[r].setOnCheckedChangeListener((buttonView, isChecked) -> {
                        if (isChecked) {
                            if (checkd[0] < Integer.parseInt(splitMultiValue[1])){


                                //checkvl.add(ch[finalR1].getText().toString());
                                //hash_all_string.get(position).remove("value");
                                //hash_all_string.get(position).put("value", String.valueOf(checkvl));

                                //CheckBox id = findViewById(ch[finalR].getId());
                                //String CheckBoxText = id.getText().toString();
                                //Log.e("CheckBoxString", CheckBoxText);
                                checkd[0] = checkd[0] +1;

                            }else{

                                ch[finalR].setChecked(false);

                            }

                        } else {
                            ch[finalR].setChecked(false);
                            checkd[0] = checkd[0] -1;

                            //checkvl.remove(ch[finalR1].getText().toString());
                            //hash_all_string.get(position).remove("value");
                            //hash_all_string.get(position).put("value", String.valueOf(checkvl));
                        }
                    });
                }
            }

        }else{
            if (myValue.equals("NULL")){
                holder.row_list_title.setText(title);
                holder.row_list_placeholder.setVisibility(View.VISIBLE);
                holder.row_list_buttn.setVisibility(View.GONE);
                holder.row_list_placeholder.setHint(hash_get.get("placeholder").toString());
                if (type.equals("Text")){
                    holder.row_list_placeholder.setInputType(InputType.TYPE_CLASS_TEXT);
                }else if(type.equals("Int")){
                    holder.row_list_placeholder.setInputType(InputType.TYPE_CLASS_NUMBER);
                }else if(type.equals("Float")){
                    holder.row_list_placeholder.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                }
            }else {

                holder.row_list_placeholder.setVisibility(View.GONE);
                holder.row_list_buttn.setVisibility(View.VISIBLE);
                holder.row_list_title.setText(title);


                int selct = 0;

                    for (int j = 0; j < hash_all2.size(); j++) {

                        if (hash_all2.get(j).get("selected").equals("true")) {

                            selct = selct+1;

                        }
                    }

                    if (selct>0){
                        holder.row_list_buttn.setBackgroundColor(getResources().getColor(R.color.purple_500));
                        holder.row_list_buttn.setText("انتخاب شد");
                    }else{
                        holder.row_list_buttn.setText("انتخاب کنید");
                        holder.row_list_buttn.setBackgroundColor(getResources().getColor(R.color.teal_200));
                    }



                String multiValue = hash_get.get("MultiValue").toString();
                String[] splitMultiValue = multiValue.split(":");


                holder.row_list_buttn.setOnClickListener(v -> {

                        if (hash_all2.size() == 0) {

                            for (int j = 0; j < splitValue.length; j++) {
                                HashMap<String, String> hash_add2 = new HashMap<>();

                                String myValue2 = splitValue[j];
                                String[] splitValue2 = myValue2.split("=");

                                hash_add2.put("Id", String.valueOf(j));
                                hash_add2.put("name", String.valueOf(splitValue2[0]));
                                hash_add2.put("value", String.valueOf(splitValue2[1]));
                                hash_add2.put("selected", "false");
                                hash_add2.put("max", splitMultiValue[1]);

                                hash_all2.add(hash_add2);

                            }
                            adapt.notifyDataSetChanged();
                        }

                        alertDialogSelect(position);
                    });

            }
        }




    }



    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.row_form, parent, false);
        return new ViewHolder(view);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView row_list_title;
        EditText row_list_placeholder;
        LinearLayout linearLayoutCheckBox;
        Button row_list_buttn;



        ViewHolder(View itemView) {
            super(itemView);

            row_list_title = itemView.findViewById(R.id.row_list_title);
            row_list_placeholder = itemView.findViewById(R.id.row_list_placeholder);
            linearLayoutCheckBox = itemView.findViewById(R.id.mainrow);
            row_list_buttn = itemView.findViewById(R.id.row_list_buttn);

        }
    }
}

please help.i tryed many code from stack but not working

holder.setIsRecyclable(false); setHasStableIds(true);

this is not working for me


Solution

  • In RecyclerView when scrolling state of view removed and creating new view by scroll,

    you must change your data when radioButton or edittext content changed, and set new data in your model.