Reputation: 1
I'm new to Android. I want to create a group of radiobuttons in application that I'm working on, but I want to define the number of radiobuttons from csv file or database. In a way that if I have two options in the csv file, show me 2 radiobutton and If I have 3 options in the csv file show me 3 radiobuttons. How can i achieve this?
I'm using eclipse
Upvotes: 0
Views: 241
Reputation: 1
Peace on you
to set dynamically created Radio buttons into a RadioGroupfor I find this solution
http://androiddesk.wordpress.com/2012/08/05/creating-dynamic-views-in-android/
and to parce the csv file I've used this fonction
private String[] loadArrayFromFileName(){
String[] liste=null;
String[] liste2=null;
String liste3=null;
int s=0;
try {
// Get input stream and Buffered Reader for our data file.
InputStream is = FocusTow.this.getAssets().open("Test.csv");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
//Read each line
while ((line = reader.readLine()) != null) {
liste2=line.split("\n");
for(int i=0; i< liste2.length;i++){
if(s==0) {
liste3=liste2[i];
s=1;
}
else liste3=liste3+","+liste2[i];
}
}
liste=liste3.split(",");
//Read each line
} catch (IOException e) {
e.printStackTrace();
}
return liste;
}
and this is the code of my button listener
mybutton.setOnClickListener(new OnClickListener() {
int j=0;
final String[] liste=loadArrayFromFileName();
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
for( j=0;j<liste.length/2;j++){
RadioButton radiobutton=new RadioButton(getApplicationContext());
radiobutton.setText(liste[j*2+1]);
radioGroup.addView(radiobutton);
}
}
});
I hope this help u, Thanks.
Upvotes: 0
Reputation: 324
1) read the csv file: How to read csv file in android?
2) in your application add for each option a radiobutton dynamically: how to set dynamically created Radio buttons into a RadioGroup?
Upvotes: 1