简体   繁体   English

如何从 stepstone 存储 id 选择的单选按钮

[英]How to store id selected radiobutton from stepstone

I'm creating an app where I have radio group with three radio buttons.我正在创建一个应用程序,其中包含带有三个单选按钮的单选组。 What I want is when I check the radio button I need to get the id value of that radio button and save it in array or database and then count the id value.我想要的是当我检查单选按钮时,我需要获取该单选按钮的 id 值并将其保存在数组或数据库中,然后计算 id 值。

For example i have this radiogroup in 19 fragment.例如,我在 19 个片段中有这个无线电组。 which is each radiobutton has id value.这是每个单选按钮都有 id 值。 if the button selected, it will get the id and store it so i can count the selected id value.如果选择了按钮,它将获取 id 并存储它,以便我可以计算选定的 id 值。 this is the display of my stepperstone这是我的踏脚石的展示

My StepFragment我的 StepFragment

Bundle bundle = this.getArguments();
        Log.d("##", "onCreateView: " + bundle.getInt(MyStepperAdapter.CURRENT_STEP_POSITION_KEY)) ;

        View view = inflater.inflate(R.layout.fragment_step_fragment_example, container, false);

        mRadioGroup = view.findViewById(R.id.radioGroupWrapper);
        mRadioGroup.getCheckedRadioButtonId();

        DatabaseHelper mDBHelper = new DatabaseHelper(getContext());
        Log.d("#######", String.valueOf(bundle.getInt(MyStepperAdapter.CURRENT_STEP_POSITION_KEY)));
        final List<Klasifikasi> lk = mDBHelper.getListKlasifikasiByGroup(String.valueOf(Integer.valueOf(bundle.getInt(MyStepperAdapter.CURRENT_STEP_POSITION_KEY)) + 1));
        final RadioButton[] rb = new RadioButton[lk.size()];
        Log.d("##sz", "onCreateView: " + lk.size()) ;


        int[][] states = new int[][] {
                new int[] { android.R.attr.state_enabled}, // enabled
                new int[] {-android.R.attr.state_enabled}, // disabled
                new int[] {-android.R.attr.state_checked}, // unchecked
                new int[] { android.R.attr.state_pressed}  // pressed
        };

        int[] colors = new int[] {
                Color.BLACK,
                Color.BLACK,
                Color.BLACK,
                Color.BLACK
        };

        mRadioButton1 = view.findViewById(R.id.checkbox1);
        mRadioButton2 = view.findViewById(R.id.checkbox2);
        mRadioButton3 = view.findViewById(R.id.checkbox3);

        if(lk.size() > 0) {
            Klasifikasi kl0 = lk.get(0);
            Klasifikasi kl1 = lk.get(1);
            Klasifikasi kl2 = lk.get(2);
            mRadioButton1.setText(kl0.getKlasifikasi());
            mRadioButton2.setText(kl1.getKlasifikasi());
            mRadioButton3.setText(kl2.getKlasifikasi());
        }

My StepperAdapter我的步进适配器

public static final String CURRENT_STEP_POSITION_KEY = "CURRENT_STEP_POSITION_KEY";

    public MyStepperAdapter(FragmentManager fm, Context context) {
        super(fm, context);
    }

    @Override
    public Step createStep(int position) {
        final StepFragmentExample step = new StepFragmentExample();
        Bundle b = new Bundle();
        b.putInt(CURRENT_STEP_POSITION_KEY, position);
        step.setArguments(b);
        return step;
    }

    @Override
    public int getCount() {
        return 19;
    }

    @NonNull
    @Override
    public StepViewModel getViewModel(@IntRange(from = 0) int position) {
        //Override this method to set Step title for the Tabs, not necessary for other stepper types
        return new StepViewModel.Builder(context)
                .setTitle("Fragment " + getCount()) //can be a CharSequence instead
                .create();
    }

ListKlasifikasiAdapter列表KlasifikasiAdapter

public ListKlasifikasiAdapter.KlasifikasiViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
        View view = layoutInflater.inflate(R.layout.item_listklasifikasi, viewGroup, false);

        KlasifikasiViewHolder klasifikasiViewHolder = new KlasifikasiViewHolder(view);
        klasifikasiViewHolder.setIsRecyclable(false);

        return klasifikasiViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull KlasifikasiViewHolder klasifikasiViewHolder, int i) {
        final List<Klasifikasi> lk = mDBHelper.getListKlasifikasiByGroup(String.valueOf(mKlasifikasiList.get(i).getGroup_index()));
        final RadioButton[] rb = new RadioButton[lk.size()];

        for (int j = 0; j < lk.size(); j++) {
            Klasifikasi kl = lk.get(j);
            rb[j] = new RadioButton(mContext);
            rb[j].setId(j);
            rb[j].setText(kl.getKlasifikasi());
            klasifikasiViewHolder.radioGroup.addView(rb[j]);
        }
    }

    class KlasifikasiViewHolder extends RecyclerView.ViewHolder {
        private RadioGroup radioGroup;

        public KlasifikasiViewHolder(@NonNull View itemView) {
            super(itemView);
            radioGroup = itemView.findViewById(R.id.radioGroupWrapper);
        }
    }

MyDatabaseHelper我的数据库助手

public List<Klasifikasi> getListKlasifikasiByGroup(String index){
        Klasifikasi klasifikasi = null;
        List<Klasifikasi> klasifikasiList = new ArrayList<>();
        openDatabase();
        Cursor cursor = mDatabase.rawQuery("SELECT * FROM klasifikasi k WHERE k.group_index = ? ORDER BY k.kategori_id ASC", new String[] {index});
        cursor.moveToFirst();
        while (!cursor.isAfterLast()){
            klasifikasi = new Klasifikasi(cursor.getInt(0),cursor.getString(1),cursor.getString(2),cursor.getString(3), cursor.getInt(4));
            klasifikasiList.add(klasifikasi);
            cursor.moveToNext();
        }
        cursor.close();
        closeDatabase();

ga ada yg jamab T^T在角落里哭泣

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM