java 根据数据库中的记录生成单选按钮

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 根据数据库中的记录生成单选按钮相关的知识,希望对你有一定的参考价值。

//in xml layout
<RadioGroup
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/RadioGroupInsurance" />

// in java code behind
ArrayList<TableInsurance.InsuranceNonStatic> InsuranceList;
RadioGroup insuranceRadioGroup;
int counter = 0;

insuranceRadioGroup =(RadioGroup)view.findViewById(R.id.RadioGroupInsurance); // in method OncreateView

public boolean GetInsurance(){
        boolean result = true;
        TableInsurance.userId = TableUser.userId;

        DBHandler DB = new DBHandler(getContext());
        InsuranceList = DB.GetInsurance(); // see method below

        if(InsuranceList.isEmpty()){
            result = false;
        }
        return result;
    }

if(InsuranceList.size() > 0){
            GenerateRadioButton(counter);
        }

public void GenerateRadioButton(int counter){
        TableInsurance.insuranceCount = InsuranceList.size();

        RadioButton[] InsuranceRadioButton = new RadioButton[TableInsurance.insuranceCount];
        insuranceRadioGroup.setOrientation(RadioGroup.VERTICAL);

        for (TableInsurance.InsuranceNonStatic insurance:  InsuranceList) {
            InsuranceRadioButton[counter]  = new RadioButton(getContext());
            InsuranceRadioButton[counter].setText(insurance.insuranceName + "  " + insurance.policyNo + " ,Expired on " + insurance.expiryDate );
            InsuranceRadioButton[counter].setId(insurance.insuranceId);
            insuranceRadioGroup.addView(InsuranceRadioButton[counter]);
        }
    }
public ArrayList<TableInsurance.InsuranceNonStatic> GetInsurance(){
        ArrayList<TableInsurance.InsuranceNonStatic> insuranceList = new ArrayList<>();

        SQLiteDatabase db = this.getWritableDatabase();
        String query;
        query  = "SELECT * FROM "
                + TableInsurance.TableInsuranceDetails.tableName  +
                " WHERE " + TableInsurance.TableInsuranceDetails.col_userId + " = " + TableUser.userId +
                " AND "
                + TableInsurance.TableInsuranceDetails.col_insuranceStatus + " = '" + TableInsurance.insuranceStatus + "'";

        Cursor cursor = db.rawQuery(query , null);
        if(cursor.getCount() > 0){
            for(cursor.moveToFirst(); !cursor.isAfterLast() ; cursor.moveToNext()){
                TableInsurance.InsuranceNonStatic insurance = new TableInsurance().new InsuranceNonStatic();

                insurance.insuranceId = Integer.parseInt(cursor.getString(0));
                insurance.userId =  Integer.parseInt(cursor.getString(1));
                insurance.insuranceType = cursor.getString(2);
                insurance.selectVehicle = Integer.parseInt(cursor.getString(3)) ;
                insurance.insuranceName = cursor.getString(4);
                insurance.policyNo =  Integer.parseInt(cursor.getString(5)) ;
                insurance.certificateNo = cursor.getString(6);
                insurance.policyHolder = cursor.getString(7);
                insurance.effectiveDate = cursor.getString(8);
                insurance.expiryDate = cursor.getString(9);
                insurance.insuranceCost = Float.parseFloat(cursor.getString(10));
                insurance.insuranceStatus = cursor.getString(11);

                insuranceList.add(insurance);
            }
        }
        db.close();

        return  insuranceList;
    }

以上是关于java 根据数据库中的记录生成单选按钮的主要内容,如果未能解决你的问题,请参考以下文章

如何根据 R Shiny 中的单选按钮显示输出?

需要根据 reactjs 中的单选按钮输入填充值

jquery循环遍历radio单选按钮,并设置选中状态

Rails 单选按钮:根据模型/数据库条目设置默认值

从ajax请求动态生成的单选按钮获取值

根据 WooCommerce 中的自定义结帐单选按钮和文本字段设置动态费用