如何存储ID从Stepstone中选择的单选按钮

时间:2019-07-01 14:31:31

标签: java android step

我正在创建一个应用程序,其中有带有三个单选按钮的单选组。我想要的是,当我检查单选按钮时,我需要获取该单选按钮的id值并将其保存在数组或数据库中,然后计算该id值。

例如,我在19个片段中有这个无线电组。每个单选按钮都有ID值。如果选择按钮,它将获取ID并将其存储,以便我可以计算所选ID值。 this is the display of my stepperstone

我的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());
        }

我的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

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

1 个答案:

答案 0 :(得分:0)

ga ada yg颌骨T ^ T 在角落哭泣