android - 从另一个活动使用 SharedPreferences 在 ArrayListAdapter 中添加项目
问题描述
我想将一些元素从另一个添加MainActivity
到另一个activity
,我有一个arrayList
但我的问题是,当我插入一些东西时,交易就完成了,但只添加了 1 个元素。我想将多个元素添加到arrayList
. 在MainActivity
我有 2EditText
和 2中buttons
(保存以及GoToNextActivity
我在哪里放置了从MainActivity
list.class 转换的意图)当我按下保存按钮时,我可以做些什么来向列表中添加更多元素?
public class items {
private String username;
private String password;
items(String user,String parola){
username=user;
password=parola;
}
public String getPassword() {
return password;
}
public String getUsername() {
return username;
}
}
public class itemsAdapter extends ArrayAdapter<items> {
private static final String LOG_TAG = itemsAdapter.class.getSimpleName();
public itemsAdapter(Activity context, ArrayList<items> item) {
super(context, 0,item);
}
public View getView(int position, View convertView, ViewGroup parent){
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_items, parent, false);
}
items curentItems=getItem(position);
TextView user=(TextView)listItemView.findViewById(R.id.list_user);
TextView password=(TextView)listItemView.findViewById(R.id.list_password);
user.setText(curentItems.getUsername());
password.setText(curentItems.getPassword());
return listItemView;
}
}
公共类列表扩展 AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
SharedPreferences sharedPreferences= getSharedPreferences("user", Context.MODE_PRIVATE);
String name=sharedPreferences.getString("username","");
String password=sharedPreferences.getString("password","");
final ArrayList<items> login = new ArrayList<items>();
login.add(new items(name,password));
itemsAdapter itemsAdapter=new itemsAdapter(this,login);
ListView listView = (ListView) findViewById(R.id.list_activity_container);
listView.setAdapter(itemsAdapter);
}
}
公共类 MainActivity 扩展 AppCompatActivity {
EditText username;
EditText password;
TextView show;
Button save;
Button display;
Button go;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username=(EditText)findViewById(R.id.username);
password=(EditText)findViewById(R.id.password);
show=(TextView)findViewById(R.id.show);
save=(Button)findViewById(R.id.save);
display=(Button)findViewById(R.id.displayInfo);
go=(Button)findViewById(R.id.goToList);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences= getSharedPreferences("user", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("username",username.getText().toString());
editor.putString("password",password.getText().toString());
editor.apply();
// Toast.makeText(this,"Saved",Toast.LENGTH_LONG).show();
}
});
display.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences= getSharedPreferences("user", Context.MODE_PRIVATE);
String name=sharedPreferences.getString("username","");
String password=sharedPreferences.getString("password","");
show.setText(name+" "+password);
}
});
go.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,list.class);
startActivity(intent);
}
});
}
}
解决方案
Shared Preferences
保存一个键值对。要保存多个元素,SharedPreferences
您需要为每个元素分配一个唯一键。让我们将我们的密钥命名为“userID”。
int userID = 0;
并将用户详细信息保存到这样的共享首选项中
SharedPreferences sharedPreferences= getSharedPreferences("user", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("username_"+userID,username.getText().toString());
editor.putString("password_"+userID,password.getText().toString());
editor.apply();
当您添加另一个用户对象时,增加 userID
++userID;
所以现在你shared preferences
将包含两个具有键<username_0>
和的元素<username_1>
。
此外,在从 获取数据时preferences
,不要忘记使用正确的密钥。
String name=sharedPreferences.getString("username_"+userID,"");
For 循环:假设你有 5 个元素并且你想将它们添加到你的List
在.
onCreate
_MainActivity
final ArrayList<items> login = new ArrayList<items>(itemCount);
for (int i = 0; i < 5; i++) {
// command below will be executed 5 times, and i will range from 0 to 4(both inclusive)
login.add(new items("name" + i, "password" + i));
}
// now our login list has 5 elements(namely name0,name1...name4)
在保存按钮的单击侦听器中,将整个保存
list
到shared preferences
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
for (int i = 0; i < 5; i++) {
// save entire list using loop.
editor.putString("username" + i, login.get(i).getUsername());
editor.putString("password" + i, login.get(i).getPassword());
}
editor.apply();
// Toast.makeText(this,"Saved",Toast.LENGTH_LONG).show();
}
});
在 中
List activity
,从首选项中读取数据。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
final ArrayList<items> login = new ArrayList<items>();
SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
for (int i = 0; i < 5; i++) {
String name = sharedPreferences.getString("username" + i, "");
String password = sharedPreferences.getString("password" + i, "");
login.add(new items(name, password));
}
itemsAdapter itemsAdapter = new itemsAdapter(this, login);
ListView listView = (ListView) findViewById(R.id.list_activity_container);
listView.setAdapter(itemsAdapter);
}
但是,在ListActivity
您不需要从中读取数据的情况下shared preferences
,您可以将数据捆绑到Intent
用于启动ListActivity
中,并ListActivity
从 Intent 中获取数据。
推荐阅读
- python - 什么是从 Python 中的一种方法在构造函数中设置多个成员的干净的 Pythonic 方式?
- python - 显示 INVALID_TRACKING_CODE 的 PayPal 跟踪
- android - 如何在 Jetpack Compose 中使用 Modifier.contentSize?
- python - sqlite3.OperationalError:没有这样的表:部门
- python - 正则表达式 - 可选文本后跟贪婪匹配的重复文本
- android - 使用视图绑定时如何处理不同布局配置中缺少的视图?
- javascript - 当我想使用 React Navigation 时出现错误,Error: requireNativeComponent: "RNSScreen" was not found in the UIManger
- python - 从一张图片过渡到另一张 pygame
- python-3.x - 训练 GAN 时出现 InvalidArgumentError
- node.js - JSON中的意外令牌o - 节点/ API