Reputation: 353
In my app you can create a work project, you add client name, budget etc. for UI purposes a staff member addition is on a seprate activity
Now My problem is that I pass the data in a Array between the activities via INTENT that data is then displayed in a listview
Now the thing I am struggling with is that every time I want to add NEW data to the Array, The Array goes back to blank and then only displays the new data
So basically you add the Staff member data to an array, it goes to the other activity and passes that array via intent and Displays in a ListView
But it only displays the new data in the array
What I want to achieve is that the older data in the array must not disapear and the new data must just be added to the array and not create a new array therby displaying the old and the new data
Activity 1 Code
This Passes the data to the other activity aswell as starts the other activity
AddStaff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MemberBudgetPassData= MemberBudget.getText().toString();
BudgetHoursPassData = budgetHours.getText().toString();
StaffList = Client + Rates + Staff + BudgetHoursPassData + MemberBudgetPassData;
StaffArray.add(StaffList);
PassData();
}
});
public void PassData(){
Intent GoToReport=new Intent(getApplicationContext(), createProject.class);
MemberBudgetPassData= MemberBudget.getText().toString();
BudgetHoursPassData = budgetHours.getText().toString();
Toasty.info(popupActivity.this, StaffArray.toString(),Toasty.LENGTH_LONG).show();
GoToReport.putStringArrayListExtra("Array",StaffArray);
startActivity(GoToReport);
Animatoo.animateCard(popupActivity.this);
}
Activity 2
Here it retrieves the data and diplays it in a listview
Array = new ArrayList<>();
Array = getIntent().getStringArrayListExtra("Array");
final ArrayAdapter arrayAdapter =new ArrayAdapter<>
(this, android.R.layout.simple_list_item_1, Array);
if (Array == null)
{
Toasty.info(createProject.this,"Select a Staff Memebr",Toasty.LENGTH_LONG).show();
}
else
{
StaffListView.setAdapter(arrayAdapter);
}
Upvotes: 1
Views: 72
Reputation: 171
I use Gson library like this situation. Firstly, you should use model class and then pass model class or Arraylist object as String. For example;
Activity 1
public void PassData(){
Intent GoToReport=new Intent(getApplicationContext(), createProject.class);
MemberBudgetPassData= MemberBudget.getText().toString();
BudgetHoursPassData = budgetHours.getText().toString();
Toasty.info(popupActivity.this, StaffArray.toString(),Toasty.LENGTH_LONG).show();
GoToReport.putString("Array",new Gson.toJson(StaffArray));
startActivity(GoToReport);
Animatoo.animateCard(popupActivity.this);
}
Activity 2
Type listType = new TypeToken<ArrayList<StaffArray>>(){}.getType();
Array = new ArrayList<>();
Array = new Gson.fromJson(getIntent().getString("Array", listType));
final ArrayAdapter arrayAdapter =new ArrayAdapter<>
(this, android.R.layout.simple_list_item_1, Array);
if (Array == null)
{
Toasty.info(createProject.this,"Select a Staff Memebr",Toasty.LENGTH_LONG).show();
}
else
{
StaffListView.setAdapter(arrayAdapter);
}
Finally, don't remember add Gson library in gradle file.
implementation 'com.google.code.gson:gson:2.8.6'
Upvotes: 1