将 SharePreference 字符串保存到 ListView [英] Save SharePreference string to a ListView
问题描述
我有两个活动.假设A"和B",活动A"有一个名为Get"的按钮,它在每次点击后生成一个新字符串并一次显示一个.活动A"还有一个按钮发送",它将生成的字符串发送到另一个活动B".当活动B"收到数据时,它必须将数据填充到 ListView 中.
I have two activities. Let's say "A" and "B", Activity "A" has a button called "Get" which generates a new string after each click and displays them one at a time. And the activity "A" also has a button "send" which will send the generated strings to another activity "B". When the activity "B" receives the data, it has to populate the data into a ListView.
以下是简单的机器步骤:
Here's the simple step by step machine steps:
In Activity "A"
1 - Generate 1 string by pressing the generate button
2 - text will be displayed in textView
3 - "send" button will send the text from textView to another activity "B" ( I'm using sharedPreference)
In Activity "B"
4 - receive the data sent by Activity "A" ( I'm using sharedPreference)
5 - populate string into a listview
6 - go to step 1 again.
我的问题是,每当我单击发送按钮时,它都会将字符串发送到活动B",但是当我再次执行相同的任务时,它只会填充最后生成的字符串.换句话说,我的代码只填充一个字符串.
My trouble is that whenever I click the send button, it will send the string to Activity "B" but when I do the same task again, it will only populate the last generated string. In other words, my code only populates one string.
到目前为止我已经尝试了以下代码,任何帮助将不胜感激.
I have tried following code so far, any help would be appreciated.
@Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.generateButton:
generateString();
break;
case R.id.send:
//displayText is a textView
String text = displayText.getText().toString();
// data sharing using sharedPref
SharedPreferences.Editor editor = getApplicationContext().getSharedPreferences("lado", MODE_PRIVATE).edit();
editor.putString("mug",text);
editor.apply();
break;
}
---------
活动 B
ListView textList = (ListView) findViewById(R.id.list);
SharedPreferences prefs = getSharedPreferences("lado", MODE_PRIVATE);
final String text = prefs.getString("mug", "No name defined");
String[] list = new String[]{name};
// Create a List from String Array elements
final List<String> fav_list = new ArrayList<>(Arrays.asList(list));
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,list);
// Assign adapter to ListView
textList.setAdapter(adapter);
推荐答案
添加以下模型:
Dictionary.java
class Dictionary implements Serializable {
private List<Words> wordsList = new ArrayList<>();
List<Words> getWordsList() {
return wordsList;
}
void setWordsList(List<Words> wordsList) {
this.wordsList = wordsList;
}
}
Words.java
public class Words implements Serializable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
活动 A
@Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.generateButton:
generateString();
break;
case R.id.send:
//displayText is a textView
String text = generateButton.getText().toString();
SharedPreferences pref = this.getSharedPreferences("lado", Context.MODE_PRIVATE);
Words words = new Words();
words.setName(text);
List<Words> wordsList = new ArrayList<>();
Gson gson = new Gson();
Dictionary dictionary = gson.fromJson(pref.getString("word", ""), Dictionary.class);
if (dictionary != null && dictionary.getWordsList() != null) {
wordsList.addAll(dictionary.getWordsList());
dictionary.setWordsList(wordsList);
wordsList.add(words);
} else {
dictionary = new Dictionary();
wordsList.add(words);
dictionary.setWordsList(wordsList);
}
SharedPreferences.Editor editor = pref.edit();
editor.putString("word", new Gson().toJson(dictionary));
editor.commit();
break;
}
活动 B
SharedPreferences pref = getSharedPreferences("lado", MODE_PRIVATE);
List<String> list = new ArrayList<>();
Gson gson = new Gson();
Dictionary dictionary = gson.fromJson(pref.getString("word", ""), Dictionary.class);
if (dictionary != null)
for (Words words : dictionary.getWordsList()) {
list.add(words.getName());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, list);
// Assign adapter to ListView
textList.setAdapter(adapter);
//**Remove when click on item**
textList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Gson gson = new Gson();
Dictionary dictionary = gson.fromJson(pref.getString("word", ""), Dictionary.class);
for (Words words :
dictionary.getWordsList()) {
if (words.getName().equalsIgnoreCase(list.get(i))) {
dictionary.getWordsList().remove(words);
list.remove(i);
break;
}
}
SharedPreferences.Editor editor = pref.edit();
editor.putString("word", new Gson().toJson(dictionary));
editor.commit();
adapter.notifyDataSetChanged();
}
});
这篇关于将 SharePreference 字符串保存到 ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!