Home > Software design >  In what manner are values added to a list in android studio?
In what manner are values added to a list in android studio?

Time:04-05

I have been getting this problem when trying to retrieve data from the database: I save the data into 'new recipe' and then add that to the list, however, whenever I try to get the value from an index in that list, it says it is out of index range in logcat(java.lang.IndexOutOfBoundsException: Index: 1, Size: 1), even though the index is 1. This leads me to ask: When adding to a list in this way, are all the variables stored in one item in the list rather than 8 separate ones?

Here's the code for reading and adding to the list (inDBHelper Class):

 public List<RecipeModel> readRecipe() {
        SQLiteDatabase db = this.getReadableDatabase();
        List<RecipeModel> returnRecipe = new ArrayList<>();

        String queryString = "SELECT * FROM "   RECIPE_TABLE;

        Cursor cursor = db.rawQuery(queryString, null);

        cursor.moveToLast();
        int recipeID = cursor.getInt(0);
        String recipeName = cursor.getString(1);
        String recipeCountry = cursor.getString( 2);
        String recipeHours = cursor.getString(3);
        String recipeMinutes = cursor.getString(4);
        String recipeEquipment = cursor.getString(5);
        String recipeIngredients = cursor.getString(6);
        String recipeMethod = cursor.getString(7);
        String recipeStory = cursor.getString(8);

        RecipeModel newRecipe = new RecipeModel(recipeID, recipeName, recipeCountry, recipeHours, recipeMinutes, recipeEquipment, recipeIngredients, recipeMethod, recipeStory);

        returnRecipe.add(newRecipe);
        cursor.close();
        db.close();
        return returnRecipe;

    }

Here is the code that is trying to get the value from the list index (in Main Activity):

 DBHelper dbHelper = new DBHelper(preview_recipe.this);
        //call read function and array
        List<RecipeModel> returnRecipe = dbHelper.readRecipe();

        //Setting values into text views
        viewRecipeName.setText((CharSequence) returnRecipe.get(1));
        viewCountry.setText((CharSequence) returnRecipe.get(2));
        viewTxtHours.setText((CharSequence) returnRecipe.get(3));
        viewMins.setText((CharSequence) returnRecipe.get(4));
        viewTxtEquipment.setText((CharSequence) returnRecipe.get(5));
        viewTxtIngredients.setText((CharSequence) returnRecipe.get(6));
        viewTxtMethod.setText((CharSequence) returnRecipe.get(7));
        viewPersonalStory.setText((CharSequence) returnRecipe.get(8));

RecipeModel (excluding getters and setters) :

public class RecipeModel {
    private int id;
    private String recipeName;
    private String recipeCountry;
    private String recipeHours;
    private String recipeMinutes;
    private String recipeEquipment;
    private String recipeIngredients;
    private String recipeMethod;
    private String recipeStory;

    public RecipeModel(int id, String recipeName, String recipeCountry, String recipeHours, String recipeMinutes, String recipeEquipment, String recipeIngredients, String recipeMethod, String recipeStory) {
        this.id = id;
        this.recipeName = recipeName;
        this.recipeCountry = recipeCountry;
        this.recipeHours = recipeHours;
        this.recipeMinutes = recipeMinutes;
        this.recipeEquipment = recipeEquipment;
        this.recipeIngredients = recipeIngredients;
        this.recipeMethod = recipeMethod;
        this.recipeStory = recipeStory;
    }


    @Override
    public String toString() {
        return "RecipeModel{"  
                "id="   id  
                ", recipeName='"   recipeName   '\''  
                ", recipeCountry='"   recipeCountry   '\''  
                ", recipeHours='"   recipeHours   '\''  
                ", recipeMinutes='"   recipeMinutes   '\''  
                ", recipeEquipment='"   recipeEquipment   '\''  
                ", recipeIngredients='"   recipeIngredients   '\''  
                ", recipeMethod='"   recipeMethod   '\''  
                ", recipeStory='"   recipeStory   '\''  
                '}';
    }

CodePudding user response:

The method List<RecipeModel> readRecipe() return a List who contain a single RecipeModel.

You should write :

        viewRecipeName.setText((CharSequence) returnRecipe.get(0).getName());
        viewCountry.setText((CharSequence) returnRecipe.get(0).getCountry());
        viewTxtHours.setText((CharSequence) returnRecipe.get(0).getTxtHours());
        ...

If you need just to fill the informations of a single RecipeModel , change the helper Method return type is better , i suggest that you make it return a RecipeModel not a List of RecipeModels.

  • Related