dimanche 3 avril 2016

Not able to access entire string

I am saving a list of ingredients the user enters as a Json array and then saving that to an SQLite database.

        btnSteps.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!etSteps.getText().toString().trim().equals("")) {
                String stps = etSteps.getText().toString();
                stepsAdapter.add(stps);
                etSteps.setText("");
                updateListViewHeight(lvSteps);

                try {
                    JSONObject json = new JSONObject();
                    json.put("steps", new JSONArray(steps.toArray()));
                    arraySteps = json.toString();
                } catch (JSONException e) {
                    Log.e("error", "Unexpected JSON exception", e);
                }
            }
        }
    });

    public void onSave(View v) {
    serve();
    time();
    if (etName.getText().toString().trim().equals("")) {

    } else {
        myDb.insertRow(etName.getText().toString(), arrayIngredients, arraySteps, servesActual, timeActual);
        this.finish();
    }
}

However, when I try to retrieve it and then change it it doesn't work.

    private void getRecipe(long id) {
    Cursor cursor = myDb.getRow(id);
    ingredients = DBAdapter.KEY_INGREDIENTS;
    getIngredients();
    String[] fromFieldNames = new String[] {DBAdapter.KEY_NAME, DBAdapter.KEY_SERVES, DBAdapter.KEY_TIME, DBAdapter.KEY_INGREDIENTS DBAdapter.KEY_STEPS};
    int[] toViewIDs = new int[] {R.id.showName, R.id.showServes, R.id.showTime, R.id.textIngredients, R.id.showSteps};
    SimpleCursorAdapter myCursorAdapter;
    myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.recipe_layout, cursor, fromFieldNames, toViewIDs, 0);
    recipeDisplay.setAdapter(myCursorAdapter);
}

    private void getIngredients() {
    seperated = ingredients.toString();
    seperated = seperated.replace("ingredients","test");
    textIngredients.setText(seperated);
}

When I do this it only gives me "ingredients" and not the other strings entered by the user.

I've checked the SQLite database and they are being saved in this format {"ingredients":["example_one","example_two","example_three"]}.

I want to be able to display just the example information but I'm can't find a way to access it.

Thank you.

Aucun commentaire:

Enregistrer un commentaire