0

I'm trying to make an app where you can add an item to a list using an EditText and a ListView. It has to remember what you added to the list because there are multiple activities, so I'm using SharedPreferences. The first time the activity opened it crashed because it looked for one of the SharedPreferences files but it wasn't there, so I added

if (newquestion == null) {
        return;
    }

but now it never adds anything to the list.

My full code for this activity:

package com.example.dogwise;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView;
import android.content.SharedPreferences;

public class Questions extends AppCompatActivity {
    ListView listview;
    String[] ListElements = new String[]{
            "How much food should I feed my dog?",
            "How do I teach my dog to sit?"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_questions);

        listview = (ListView) findViewById(R.id.QuestionsList);

        final List<String> ListElementsArrayList = new ArrayList<String>(Arrays.asList(ListElements));
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (Questions.this, android.R.layout.simple_list_item_1, ListElementsArrayList);
        listview.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
        String newquestion=pref.getString("key_question", null);

        if (newquestion == null) {
            return;
        }

        ListElementsArrayList.add(newquestion);
        adapter.notifyDataSetChanged();

        Bundle newQuestion = getIntent().getExtras();
        if (newQuestion == null) {
            return;
        }
        final String QuestionName = newQuestion.getString("QuestionName");

        ListElementsArrayList.add(QuestionName);
        adapter.notifyDataSetChanged();

        SharedPreferences.Editor editor = getSharedPreferences("MyPref", MODE_PRIVATE).edit();
        editor.putString("key_question", QuestionName);

        editor.apply();

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                if (position == 0) {
                    Intent myIntent = new Intent(view.getContext(), Question_1.class);
                    startActivityForResult(myIntent, 0);
                }

                if (position == 1) {
                    Intent myIntent = new Intent(view.getContext(), Question_2.class);
                    startActivityForResult(myIntent, 0 );
                }

                if (position == 2) {
                    Intent myIntent = new Intent(view.getContext(), Question_Asked.class);
                    myIntent.putExtra("QuestionTitle", QuestionName);
                    startActivityForResult(myIntent, 0);
                }
            }
        });
    }

    ;

    public void backHomeOnClick(View view) {
        Intent b = new Intent(this, HomeScreen.class);
        startActivity(b);
    }

    public void askAQuestionOnClick(View view) {
        Intent i = new Intent(this, AskAQuestion.class);
        startActivity(i);
    }

    ;
}
Sylvie
  • 23
  • 5

1 Answers1

0

You are publishing your data into an intent using key= "QuestionTitle" but trying to retrieve that using key= "QuestionName".

G-dev
  • 11
  • 2