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 根据数据库中的记录生成单选按钮的主要内容,如果未能解决你的问题,请参考以下文章