Skye
Skye

Reputation: 11

Populate ListView using Shared preferences

I have a problem in populating the listview in my second activity using shared preferences. When I add a new value in my editText in the first activity the second activity only display a listview with a single item that contains arraylist I entered.Please help me solve this problem.

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;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;


public class History extends AppCompatActivity {
Button translate;
Button next;
EditText enterText;
private SharedPreferences hPreferences;
ArrayList<String> hisList = new ArrayList<String>();

@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);

            String input = enterText.getText().toString();
            if(input == null){
                Toast.makeText(getBaseContext(),"Input Field is empty",Toast.LENGTH_SHORT).show();
            }else
            {
                hisList.add(input);
                hPreferences  = getSharedPreferences("SaveHistory",Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = hPreferences.edit();
                Set<String> set = new HashSet<String>();
                set.addAll(hisList);
                editor.putStringSet("history",set);
                editor.commit();
                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;
import java.util.List;
import java.util.Set;


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 = findViewById(R.id.listView);
    listView.setAdapter(adapter);
    SharedPreferences transHistory  = getSharedPreferences("SaveHistory",Context.MODE_PRIVATE);
    Set<String> tHistory = transHistory.getStringSet("history", null);
    List<String> sample=new ArrayList<String>(tHistory);
    itemList.add(String.valueOf(sample));
    adapter.notifyDataSetChanged();


}

}

Upvotes: 1

Views: 392

Answers (1)

Elsunhoty
Elsunhoty

Reputation: 1627

Your problem is here

itemList.add(String.valueOf(sample));

Replace it with

for (String x: tHistory) {
    itemList.add(x);
}

Upvotes: 2

Related Questions