Java Android:ListView仅显示共享首选项保存的最后一项
为了清楚起见,ma.getData()从另一个活动返回一个字符串,我想将其保存在共享首选项中。因此,每次我将字符串(来自其他活动)发送到以下活动时,我都会使用saveData()保存它(单击按钮时会调用此函数)。。因此,我使用另外两个函数(loadData()和updateViews())在listview中显示了许多字符串。这些函数在活动启动时被调用,以便在listview中加载保存的字符串。 问题是listView中只显示我从其他活动中获取的最后一个字符串 我做错了什么Java Android:ListView仅显示共享首选项保存的最后一项,java,android,list,file,listview,Java,Android,List,File,Listview,为了清楚起见,ma.getData()从另一个活动返回一个字符串,我想将其保存在共享首选项中。因此,每次我将字符串(来自其他活动)发送到以下活动时,我都会使用saveData()保存它(单击按钮时会调用此函数)。。因此,我使用另外两个函数(loadData()和updateViews())在listview中显示了许多字符串。这些函数在活动启动时被调用,以便在listview中加载保存的字符串。 问题是listView中只显示我从其他活动中获取的最后一个字符串 我做错了什么 public cla
public class activity_profile extends AppCompatActivity {
MainActivity ma = new MainActivity();
private static ArrayList<String> addArray;
ListView show;
private Button btn;
private ArrayAdapter<String> adapter;
public static final String SHARED_PREFS = "sharedPrefs";
public static final String TEXT = "text";
private String text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
mEditText = (TextView) findViewById(R.id.hist) ;
show = (ListView) findViewById(R.id.listview1);
namefield = (EditText) findViewById(R.id.username);
namelabel = (TextView) findViewById(R.id.namelabel);
btn = (Button)findViewById(R.id.Update_btn);
loadData();
updateViews();
}
public void saveData() {
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(TEXT, ma.getData());
editor.apply();
Toast.makeText(this, "Data saved", Toast.LENGTH_SHORT).show();
}
public void loadData() {
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
text = sharedPreferences.getString(TEXT, "");
}
public void updateViews() {
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, Collections.singletonList(text));
show.setAdapter(adapter);
//mEditText.setText(sb.toString());
adapter.notifyDataSetChanged();
}
公共类活动\u配置文件扩展了AppCompatActivity{
MainActivity ma=新的MainActivity();
私有静态数组列表addArray;
列表视图显示;
专用按钮btn;
专用阵列适配器;
公共静态最终字符串SHARED\u PREFS=“sharedPrefs”;
公共静态最终字符串TEXT=“TEXT”;
私有字符串文本;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
mEditText=(TextView)findViewById(R.id.hist);
show=(ListView)findViewById(R.id.listview1);
namefield=(EditText)findViewById(R.id.username);
namelabel=(TextView)findViewById(R.id.namelabel);
btn=(按钮)findViewById(R.id.Update\u btn);
loadData();
updateViews();
}
公共void saveData(){
SharedPreferences SharedPreferences=getSharedPreferences(共享优先,模式专用);
SharedReferences.Editor=SharedReferences.edit();
putString(TEXT,ma.getData());
editor.apply();
Toast.makeText(这是“保存的数据”,Toast.LENGTH_SHORT).show();
}
公共void loadData(){
SharedPreferences SharedPreferences=getSharedPreferences(共享优先,模式专用);
text=SharedReferences.getString(text,“”);
}
public void updateViews(){
adapter=newarrayadapter(getApplicationContext(),android.R.layout.simple\u spinner\u项,Collections.singletonList(text));
show.setAdapter(适配器);
//mEditText.SETEXT(某人的字符串());
adapter.notifyDataSetChanged();
}
这是我的列表视图的xml
<RelativeLayout
android:layout_width="398dp"
android:layout_height="204dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/RecipeHistoryLabel">
<ListView
android:id="@+id/listview1"
android:layout_width="386dp"
android:layout_height="wrap_content" />
</RelativeLayout>
您正在SharedPref中的单个键上覆盖数据。 你的选择是:
//saving new data on previus key
//Use - to sepete data e.g. (FirstData-SecondData-ThiredData)
String yourNewData = "data";
String oldDataFromPrefs = prefs.getString("KEY");
String dataToBeSaved = oldDataFromPrefs + "-" + yourNewData;
//reading data as String[] to show it on the ListView
//You need to pass tokens to your adapter
String readDataFromPref = prefs.getString("KEY");
String[] tokens = readDataFromPref.split("-");