Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
90 views
in Technique[技术] by (71.8m points)

Read a list from a text file and add the list to a radio group- Android

Good day

I'm building a Android App to read recipes. I want to store the recipes in text files and be able to retrieve them. I will have one file recipes.txt to list all the available recipes. I want to read recipes.txt and add the list to a array(to easily access the recipes again) and then in turn to a radio group so that the user can select the recipe, but I also want to be able to easily add another text file with another recipe(so the text files have to be stored in a location that the Android File Manager can access)

Here is the code I have so far:

public int getNoRecipes(){
    InputStream inputStream = getResources().openRawResource(R.raw.recipes);
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    //StringBuilder sb = new StringBuilder();
    int Nolines = 0;
    while (true){
        try {
            if (bufferedReader.readLine() == null) break;
        } catch (IOException e) {
            e.printStackTrace();
        }
        Nolines++; }
    return Nolines;
}
//Read recipes.txt
public ArrayList<String> getRecipeList() {
    int NoRecipes = getNoRecipes();
    ArrayList<String> Recipe_List = new ArrayList<String>();
    InputStream inputStream = getResources().openRawResource(R.raw.recipes);
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    for(int i = 0; i < NoRecipes; i++){
        try {
            Recipe_List.add(bufferedReader.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if (inputStream != null){
        try {
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return Recipe_List;
}

RadioGroup radgrpRecipes;
ArrayList<String> Recipe_List = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recipe__select);
    radgrpRecipes = findViewById(R.id.radgrpRecipes);
    try {
        addRadioButtons();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void addRadioButtons() throws IOException {
    Recipe_List = getRecipeList();
    radgrpRecipes.setOrientation(LinearLayout.HORIZONTAL);
    //Add Recipes
    for (int i =1; i<=Recipe_List.size(); i++){
        RadioButton rdbtn = new RadioButton(this);
        rdbtn.setId(View.generateViewId());
        rdbtn.setText(Recipe_List.get(i-1));
        rdbtn.setOnClickListener((View.OnClickListener) this);
        radgrpRecipes.addView(rdbtn);
    }
}
question from:https://stackoverflow.com/questions/65870260/read-a-list-from-a-text-file-and-add-the-list-to-a-radio-group-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...