I tried to initialize a custom array in Main Activity. I want to update/edit the element in array from another activity which contains the data and button click. My main activity is Main Activity and my second activity is Display. Can anyone help?
public class MainActivity extends AppCompatActivity {
public Pets[] arrayList=new Pets[]{
new Pets(R.drawable.dog,"Dog","Wolf","The dog is a domesticated descendant of the wolf. Also called the domestic dog"),
new Pets(R.drawable.bird,"Bird","Unknown!","Birds are a group of warm-blooded vertebrates constituting the class Aves characterised by feathers, toothless beaked jaws"),
new Pets(R.drawable.cat,"Cat","Tiger","The cat is a domestic species of small carnivorous mammal"),
};
public class Pets {
private int mPetImage;
private String mPetName;
private String mPetFam;
private String mDesc;
public Pets(int PetImage,String PetName,String PetFam,String Desc) {
this.mPetImage = PetImage;
this.mPetName = PetName;
this.mPetFam = PetFam;
this.mDesc = Desc;
}
public int getmPetImage() {
return mPetImage;
}
public Pets setmPetImage(int mPetImage) {
this.mPetImage = mPetImage;
return this;
}
public String getmPetName() {
return mPetName;
}
public Pets setmPetName(String mPetName) {
this.mPetName = mPetName;
return this;
}
public String getmPetFam() {
return mPetFam;
}
public Pets setmPetFam(String mPetFam) {
this.mPetFam = mPetFam;
return this;
}
public String getmDesc() {
return mDesc;
}
public Pets setmDesc(String mDesc) {
this.mDesc = mDesc;
return this;
}
}
CodePudding user response:
To update an element in the array from another activity, you can use an Intent to pass the data from the second activity (Display) to the first activity (MainActivity).
In your Display activity, you can create a button that sends an Intent to the MainActivity and includes the data you want to update in the Intent. You can then retrieve this data in the MainActivity and use it to update the appropriate element in the array.
Here's an example of how you can do this:
In the Display activity, create a button that sends an Intent to the MainActivity when clicked. You can do this by setting an OnClickListener for the button and calling the startActivity() method with the Intent as an argument:
Button updateButton = (Button) findViewById(R.id.update_button);
updateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Display.this, MainActivity.class);
// Add the data you want to update to the Intent here
startActivity(intent);
}
});
In the MainActivity, override the onNewIntent() method to retrieve the data from the Intent and update the element in the array. You can do this by calling the getIntent() method to get the Intent that was used to start the activity and extracting the data from the Intent:
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// Retrieve the data from the Intent here
// Use the data to update the appropriate element in the array
}
CodePudding user response:
First of all do not create data inside the MainActivity. Activity is your view, if you separate your data source from view you can update it very easily.