lundi 21 février 2022

reload recyclerview adapter in android

i make some checkbox and radiobutton programically in my recyclerview and when i use notifydatasetchanged all of them are replaced with items in mylist what should i do?

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



        Log.d("_hdev",hash_all.get(position) + " ::::::::: " + hash_get+"");

        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("\\|");

        // = new String[0];
        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);

            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) {
                Log.d("RadioString", "Nothing selected");
            } 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();
                        Log.d("RadioString", radioText);
                    } else {
                        Log.d("RadioString", "nothing");
                    }
                }
            }

            }else{

                //splitMultiValue[1]
                holder.row_list_title.setText(title);
                CheckBox[] ch;
                final int[] checkd = {0};
                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;
                    ch[r].setOnCheckedChangeListener((buttonView, isChecked) -> {
                        if (isChecked) {
                            Log.d("_hdev", checkd[0] +"");
                            Log.d("_hdev",Integer.parseInt(splitMultiValue[1])+"");
                            if (checkd[0] < Integer.parseInt(splitMultiValue[1])){

                                //CheckBox id = findViewById(ch[finalR].getId());
                                //String CheckBoxText = id.getText().toString();
                                //Log.e("CheckBoxString", CheckBoxText);
                                checkd[0] = checkd[0] +1;
                                Log.d("_hdevn", "1");
                            }else{
                                //CheckBox id = (CheckBox) findViewById(ch[finalR].getId());
                                ch[finalR].setChecked(false);
                                //checkd[0] = checkd[0] -1 ;
                                Log.d("_hdevn", "2");
                            }

                        } else {
                            ch[finalR].setChecked(false);
                            checkd[0] = checkd[0] -1;
                            Log.d("_hdevn", "3");

                    }
                    });
                }




            }

        }else{
            if (myValue.equals("NULL")){
                holder.row_list_title.setText(title);
                holder.row_list_placeholder.setVisibility(View.VISIBLE);
                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);
                   /*

                    TextView[] tv;
                    final int[] checkd = {0};
                    tv = new TextView[splitValue.length];

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


                        Log.d("_hdev", hash_all2.get(j).get("selected"));
                        if (hash_all2.get(j).get("selected").equals("true")) {
                            String myValue2 = splitValue[j];
                            String[] splitValue2 = myValue2.split("=");
                            tv[j] = new TextView(getApplicationContext());
                            holder.linearLayoutCheckBox.addView(tv[j]);
                            tv[j].setId(j);
                            tv[j].setText(hash_all2.get(j).get("name"));
                        }


                    }*/

                    //list popup
                    holder.row_list_title.setText(title);
                    holder.row_list_title.setOnClickListener(v -> {




                        profileadapter.notifyDataSetChanged();
                        //get_profiles();

                        /*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_all2.add(hash_add2);

                            }
                            adapt.notifyDataSetChanged();
                        }

                        alertDialogSelect();*/
                    });
                
            }
        }




    }



    @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;
        CardView cardrow;


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

        }
    }
}

this is what i tryed in many days.




Aucun commentaire:

Enregistrer un commentaire