使用共享偏好设置填充ListView

时间:2018-03-11 10:37:54

标签: android listview sharedpreferences

我在使用共享首选项填充第二个活动中的listview时遇到问题。当我在第一个活动的editText中添加一个新值时,第二个活动只显示一个listview,其中包含一个包含我输入的arraylist的项目。请帮我解决这个问题。

第一项活动

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



}

} 第二项活动

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


}

}

1 个答案:

答案 0 :(得分:1)

你的问题在这里

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

将其替换为

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