gpt4 book ai didi

android - 从另一个 Activity 使用 SharedPreferences 在 ArrayListAdapter 中添加项目

转载 作者:行者123 更新时间:2023-11-30 04:58:36 24 4
gpt4 key购买 nike

我想将 MainActivity 中的一些元素添加到另一个 activity,其中我有一个 arrayList 但我的问题是,当我插入事情已经处理好了,但只是添加了 1 个元素。我想向 arrayList 添加多个元素。在 MainActivity 中,我有 2 个 EditText 和 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;
}
}

公共(public)类列表扩展 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);

}
}

公共(public)类 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 中保存多个元素您需要为每个元素分配一个唯一的键。让我们将 key 命名为“userID”。

int userID = 0;

and save user details to shared preferences like this

 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 获取数据, 不要忘记使用正确的 key 。

String name=sharedPreferences.getString("username_"+userID,"");

For 循环:假设你有 5 个元素,你想将它们添加到你的 List 中。

in onCreate of 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)

In click listener of save button, save entire list to 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();
}
});

in List activity , read data from preferences.

 @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 中 bundle 数据用于启动ListActivity ,并在 ListActivity从 Intent 获取数据。

关于android - 从另一个 Activity 使用 SharedPreferences 在 ArrayListAdapter 中添加项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58674567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com