我想将pictureActivity.java数组传递给另一个类imageAdapter.java。 我确实在toast上获得了imgPath结果,但我不知道如何将字符串移动到另一个类。
PictureActivity.java
ArrayList<HashMap<String, String>> imgList = new ArrayList<HashMap<String, String>>();
try {
imageLink = json.getJSONArray(TAG_IMG);
for(int i = 0; i < imageLink.length(); i++){
JSONObject c = imageLink.getJSONObject(i);
String imgPath = c.getString(TAG_PATH);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_PATH, imgPath);
imgList.add(map);
Toast.makeText(this, map.get(TAG_PATH), Toast.LENGTH_LONG).show();
}
}catch (JSONException e) {
e.printStackTrace();
}
((Gallery) findViewById(R.id.gallery))
.setAdapter(new ImageAdapter(this));
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context myContext;
public String[] myRemoteImages = {
//How to add imagepath here??
};
我想将数组字符串放在myRemoteImages上。
答案 0 :(得分:1)
您可以在初始化适配器类对象时传递数组适配器:
((Gallery) findViewById(R.id.gallery))
.setAdapter(new ImageAdapter(this, imgList));
并在你的适配器类中创建一个这样的构造函数:
public class ImageAdapter extends BaseAdapter {
ArrayList<HashMap<String, String>> data = null;
public String[] myRemoteImages;
private Context myContext;
public ImageAdapter(Context context, ArrayList<HashMap<String, String>> data){
this.mContext = context;
this.data = data;
myRemoteImages = new String[this.data.size()];
for(int i=0; i<this.data.size(); i++){
HashMap<String, String> map = this.data.get(i);
myRemoteImages[i] = map.get(TAG_PATH).toString();
}
}
}
在适配器类中获取ArrayList后,您可以从中获取所需的任何值。