I want to add some elements from MainActivity
to another activity
, where I have an arrayList
but my problem is that, When I insert something the deal is done, but is added only 1 element. I want to add multiple elements to the arrayList
. In MainActivity
i have 2 EditText
and 2 buttons
(Save and GoToNextActivity
where i put an intent for a transition from the MainActivity
to the list.class) What can i do to adding more elements to the list when i press the save button?
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 class list extends 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 class MainActivity extends 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
save a key-value pair. To save multiple elements in SharedPreferences
you need to assign a unique key for each element. Let's name our key as "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();
When you add another user object, increment userID
++userID;
So now your shared preferences
will contain two elements having keys <username_0>
and <username_1>
.
Also while getting data from preferences
, don't forget to use correct key.
String name=sharedPreferences.getString("username_"+userID,"");
For loops : Suppose you have 5 elements and you want to add them to your List
in
onCreate
ofMainActivity
.
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
toshared 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);
}
However in ListActivity
you don't need to read data from shared preferences
, you can bundle data in the Intent
used to start ListActivity
, and in ListActivity
get data from Intent.