package com.example.hussain.assignment4task1;
import android.os.Parcel;
import android.os.Parcelable;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class Image implements Parcelable
{
private String imageName;
private String date;
public ArrayList<Image> images;
public Image(String imageName, String date, ArrayList<Image> images)
{
update(imageName, date, images);
}
public void update(String imageName, String date, ArrayList<Image> images)
{
this.imageName = imageName;
this.date = date;
this.images = images;
}
public String toString()
{
String str = "Image Name: "+ imageName;
str += "\nDate: " + date;
return str;
}
/** The following block of code parcels / unparcels data for distribution between activities */
/** Describe the contents in the parcel --
* interface forces implementation */
public int describeContents()
{
return 0;
}
public void writeToParcel(Parcel out, int flags)
{
out.writeString(imageName);
out.writeString(date);
out.writeList(images);
}
public static final Parcelable.Creator<Image> CREATOR =
new Parcelable.Creator<Image>()
{
public Image createFromParcel(Parcel in)
{
return new Image(in);
}
public Image[] newArray(int size)
{
return new Image[size];
}
};
/** Private constructor called internally only */
private Image(Parcel in)
{
imageName = in.readString();
date = in.readString();
}
}
这是我的活动,我在其中插入要传递给其他活动中的图像的图像细节。
public void onBackPressed()
{
Log.i("IMAGE DETAILS", "Back Button Pressed");
storeImageDetails(); // to cover half filled forms
Intent resultIntent = new Intent(this, MainActivity.class);
// need an array list even if we put a single object only
ArrayList<Image> dataList = new ArrayList<Image>(4);
dataList.add(image);
resultIntent.putParcelableArrayListExtra("IMAGE_DATA", dataList);
setResult(RESULT_OK, resultIntent);
ImageDetailsActivity.super.onBackPressed(); // do not forget
}
/** This method will store all information entered */
private void storeImageDetails()
{
EditText imageNameText = (EditText) findViewById(R.id.imageNameText);
EditText dateText = (EditText) findViewById(R.id.dateText);
String imageName = imageNameText.getText().toString();
String date = dateText.getText().toString();
ArrayList<Image> dataList = new ArrayList<Image>();
if (image == null)
image = new Image(imageName, date, dataList);
else
image.update(imageName, date, dataList);
Log.i("IMAGE DETAILS CHANGED", image.toString());
}
我的主要活动有4张图片。点击其中一个打开一个表格,您可以在其中输入他们的详细信息,按下后退按钮,他们只在图像下方播放图像名称和日期。但这对所有图像都显示相同。我想知道如何制作它,以便只有我点击的图像才能获得传递给它的细节,而不是其他图像。因此,将数据传递给单个图像的分离是我想要的。
答案 0 :(得分:0)
我认为public ArrayList<Image> images
字段不应该是Image
类的一部分,而应该是主要活动中的字段。
尝试这样的事情:
public class MainActivity extends Activity {
public ArrayList<Image> images;
// show images list via adapter or something, and put an OnClickListener on each
private void onImageClicked(int index) {
Image image = images.get(index);
Intent intent = new Intent(this, ImageDetailsActivity.class);
intent.putExtra("IMAGE", image);
intent.putExtra("INDEX", index);
startActivityForResult(intent, 12345);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 12345 && resultCode == RESULT_OK) {
int index = data.getIntExtra("INDEX", -1);
String name = data.getStringExtra("NAME");
String date = data.getStringExtra("DATE");
Image image = images.get(index);
image.update(name, date);
}
}
}
public class ImageDetailsActivity extends Activity {
public void onBackPressed() {
EditText imageNameText = (EditText) findViewById(R.id.imageNameText);
EditText dateText = (EditText) findViewById(R.id.dateText);
String imageName = imageNameText.getText().toString();
String date = dateText.getText().toString();
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.putExtra("INDEX", getIntent().getIntExtra("INDEX", -1)); // return the same index we got from Main
resultIntent.putExtra("NAME", imageName);
resultIntent.putExtra("DATE", date);
setResult(RESULT_OK, resultIntent);
super.onBackPressed();
}
}