将图像数组传递到下一个屏幕
我有一个来自数据库的动态图像数组。我只是在第一个屏幕上显示图像缩略图,并希望全屏显示同一图像阵列。问题是我想将整个动态数组传递到下一个屏幕。实际上我知道如何在java中使用首选项。但我不知道如何用变量或其他东西传递整个数组。任何想法将不胜感激。
public class MerchantDetails extends GLCityActivity implements OnClickListener {
public static MerchantDetails instance;
private Gallery gallery;
private ImageView imView;
String Header, contentName;
DBManager gDatabase = new DBManager(this);
private ArrayList<Multimedia> mm;
private ImageViewAdapter iva;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Theme_Translucent);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.details);
instance = this;
initActivity(instance, " ");
try {
gDatabase.createDataBase();
} catch (IOException e) {
throw new Error("Unable to create database");
}
try {
gDatabase.openDatabase();
} catch (SQLiteException sqle) {
throw sqle;
}
settingLayout();
}
public void settingLayout() {
SharedPreferences preferences = getSharedPreferences(
Constants.DEFAUL_SHARE_DATA, 0);
Header = preferences.getString("SubName", "");
headerTxt = (TextView) findViewById(R.id.templateTopTitleTView);
headerTxt.setText(Header);
gallery = (Gallery) findViewById(R.id.gallery);
if (mm.isEmpty()) {
gallery.setVisibility(gallery.INVISIBLE);
} else {
gallery.setAdapter(iva);
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,
int position, long id) {
SharedPreferences preferences = getSharedPreferences(
Constants.DEFAUL_SHARE_DATA, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("ImagePosition", mm.get(position) + "");
editor.putInt("ImageLength", mm.size());
Intent viewer = new Intent(instance, GalleryViewer.class);
startActivityForResult(viewer, 0);
if (editor.commit()) {
}
}
});
}
}
private class ImageViewAdapter extends ArrayAdapter<Multimedia> {
private ArrayList<Multimedia> items;
public ImageViewAdapter(Context context, int textViewResourceId,
ArrayList<Multimedia> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.image_list, null);
}
Multimedia info = items.get(position);
if (info != null) {
ImageView imView = (ImageView) v
.findViewById(R.id.rowlistIconIView);
String name = info.thumbnail;
String[] test = gDatabase.split(name, ".");
int resID = getResources().getIdentifier(test[0], "drawable",
getPackageName());
Log.v("Log", name + ";" + resID + ";" + test[0] + ";");
try {
imView.setImageResource(resID);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
return v;
}
}
}
编辑: @杰里米·爱德华兹,感谢您的回复。我尝试按照您的建议进行操作,但在编码时发现了此错误。我尝试使用 putExtra(String name,int[] value) 但错误表示 Intent 类型中的方法 putExtra(String name,boolean value) 不适用于参数 (String,Multimedia) 。实际上我已经从自动建议框中选择了 putExtra(String name,int[] value) 。想知道它怎么突然变回(String name,int[] value)。请检查下面我的代码。 私有ArrayList<多媒体>毫米; viewer.putExtra("ImagePosition",mm.get(position));
viewer.putExtra(字符串名称,int[]值)
I've a dynamic image array which come through from database. And i just show image thumbnails at first screen and want to display full screen of same image array. The thing is that i want to pass the whole dynamic array to next screen. Actually i know how to use preferences in java. But i don't know how to pass the whole array with variables or something. Any ideas would be appreciate.
public class MerchantDetails extends GLCityActivity implements OnClickListener {
public static MerchantDetails instance;
private Gallery gallery;
private ImageView imView;
String Header, contentName;
DBManager gDatabase = new DBManager(this);
private ArrayList<Multimedia> mm;
private ImageViewAdapter iva;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Theme_Translucent);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.details);
instance = this;
initActivity(instance, " ");
try {
gDatabase.createDataBase();
} catch (IOException e) {
throw new Error("Unable to create database");
}
try {
gDatabase.openDatabase();
} catch (SQLiteException sqle) {
throw sqle;
}
settingLayout();
}
public void settingLayout() {
SharedPreferences preferences = getSharedPreferences(
Constants.DEFAUL_SHARE_DATA, 0);
Header = preferences.getString("SubName", "");
headerTxt = (TextView) findViewById(R.id.templateTopTitleTView);
headerTxt.setText(Header);
gallery = (Gallery) findViewById(R.id.gallery);
if (mm.isEmpty()) {
gallery.setVisibility(gallery.INVISIBLE);
} else {
gallery.setAdapter(iva);
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,
int position, long id) {
SharedPreferences preferences = getSharedPreferences(
Constants.DEFAUL_SHARE_DATA, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("ImagePosition", mm.get(position) + "");
editor.putInt("ImageLength", mm.size());
Intent viewer = new Intent(instance, GalleryViewer.class);
startActivityForResult(viewer, 0);
if (editor.commit()) {
}
}
});
}
}
private class ImageViewAdapter extends ArrayAdapter<Multimedia> {
private ArrayList<Multimedia> items;
public ImageViewAdapter(Context context, int textViewResourceId,
ArrayList<Multimedia> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.image_list, null);
}
Multimedia info = items.get(position);
if (info != null) {
ImageView imView = (ImageView) v
.findViewById(R.id.rowlistIconIView);
String name = info.thumbnail;
String[] test = gDatabase.split(name, ".");
int resID = getResources().getIdentifier(test[0], "drawable",
getPackageName());
Log.v("Log", name + ";" + resID + ";" + test[0] + ";");
try {
imView.setImageResource(resID);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
return v;
}
}
}
Edited :
@Jeremy Edwards,thanks for your reply. I tried to do as you suggest but i found this error while i coded. I tried to use putExtra(String name,int[] value) but the error said The method putExtra(String name,boolean value) in the type Intent is not applicable for the arguments (String,Multimedia). Actually i already chose putExtra(String name,int[] value) from auto suggest box. wonder how it change back to (String name,int[] value) suddenly. Please check my code below.private ArrayList<Multimedia> mm;
viewer.putExtra("ImagePosition",mm.get(position));viewer.putExtra(String name,int[] value)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于观看者的意图。使用 Intent.put(name, "图像的 ID")。
然后在查看器活动中使用 getIntent().getIntExtra(name) 取回该 id。
我建议不要传递图像数据本身,因为 android 使用 IPC“共享内存”在活动边界之间传递数据。它本来就是轻量级的。
查看器活动应该获取 ID,然后访问数据库以获取所需的完整图像。您不需要使用首选项来完成此操作,因为 Intent 对象内的 Bundle 将携带您需要的信息。
For the viewer Intent. Use Intent.put(name, "Id of the image").
Then in the viewer activity use getIntent().getIntExtra(name) to get that id back.
I'd advise against passing image data itself since android uses IPC "Shared memory" to pass data between the activity boundaries. It was meant to be lightweight.
The viewer activity should get the ID and then hit the DB for the full blown image you want. You shouldn't need to use preferences either to accomplish this since the Bundle inside the Intent object will carry the information you need.
通过扫描你的代码,我认为你将图像存储在 ArrayList 中。
ArrayList 是可序列化的,因此您可以使用捆绑将其传递到下一个屏幕。您可以使用捆绑包的 putSerialized 方法
类似这样
,然后使用 getSerializes() 将其取回
From scanning through your code I think you are storing the images in ArrayList.
ArrayList is Serializable so you can use bundle to pass it to the next screen. You can use bundle's putSerializable method
Something like this
then use the getSerializable() to get it back