0

I am trying to create a history.I have two activities. One is for the user to input value in an editText and storing it in shared preferences and the other one is for displaying the value in listview form using shared preferences. I dont know what is the error because the app crashes.

First Activity

 package com.example.sy.list;

 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.view.inputmethod.InputMethodManager;
 import android.widget.Button;
 import android.widget.EditText;


 public class History extends AppCompatActivity {
Button translate;
Button next;
EditText enterText;
private SharedPreferences hPreferences;

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

    enterText = findViewById(R.id.editText);
    next = findViewById(R.id.btnNext);

    translate = findViewById(R.id.buttonTranslate);
    View.OnClickListener addlistener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(enterText.getWindowToken(), 0);
            hPreferences  = getSharedPreferences("SaveHistory",Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = hPreferences.edit();
            editor.putString("history",enterText.getText().toString());
            editor.apply();
            enterText.setText("");



        }

    };
    translate.setOnClickListener(addlistener);
    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(History.this,MainActivity.class);
            startActivity(intent);
        }
    });



}

}

Second Activity

 package com.example.sy.list;

 import android.content.Context;
 import android.content.SharedPreferences;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.ActionMode;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.widget.AbsListView;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;
 import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {
private ListView listView;

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

    ArrayList<String> itemList = new ArrayList<String>();
    ArrayAdapter<String> adapter;
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, itemList);
    listView.setAdapter(adapter);

    listView = findViewById(R.id.listView);
    SharedPreferences transHistory  = getSharedPreferences("SaveHistory",Context.MODE_PRIVATE);
    String tHistory = transHistory.getString("history","Data Not Found");
    itemList.add(tHistory);
    adapter.notifyDataSetChanged();


}

}

Skye
  • 11
  • 5

1 Answers1

0

You error probably is cause by this :

listView.setAdapter(adapter);

listView = findViewById(R.id.listView);

First of all, you set the adapter listView.setAdapter(adapter); but, the problem is that listView is not null since you called listView = findViewByIt(R.id.listview);

So, switch the statements, and you should be good to go

Edit

listView = findViewById(R.id.listView);

then

listView.setAdapter(adapter);
Community
  • 1
  • 1
Ionut J. Bejan
  • 734
  • 11
  • 28
  • Thank you. It works. but I've got another problem. Instead of populating the listview it just change the existing value of the single item in listview. – Skye Mar 11 '18 at 09:02
  • P.S. Sorry newbie here – Skye Mar 11 '18 at 09:03
  • So, you want the last added item to be added on top ? In this case, you should save in shared preferences not only last `String` but rather an `ArrayList` instead. And each time you add another item, just updated the array in shared preferences ;) – Ionut J. Bejan Mar 11 '18 at 09:05
  • Yes that's what I want to happen. Which part of my code should I change to ArrayList? – Skye Mar 11 '18 at 09:12
  • You can take a look at [this StackOverflow post](https://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences) to see how you can save `ArrayList` in sharedpreferences! – Ionut J. Bejan Mar 11 '18 at 09:15