Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将Arraylist字符串传递给另一个类';谁的适配器?_Android_Arrays_Arraylist_Hashmap_Adapter - Fatal编程技术网

Android 如何将Arraylist字符串传递给另一个类';谁的适配器?

Android 如何将Arraylist字符串传递给另一个类';谁的适配器?,android,arrays,arraylist,hashmap,adapter,Android,Arrays,Arraylist,Hashmap,Adapter,我想将pictureActivity.java数组传递给另一个类imageAdapter.java。 我确实在toast上得到了imgPath结果,但我不知道如何将字符串移动到另一个类 PictureActivity.java ArrayList<HashMap<String, String>> imgList = new ArrayList<HashMap<String, String>>(); try { imageL

我想将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));

我想将数组字符串放在myRemoteImages上。

初始化适配器类对象时,可以传递数组适配器:

((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();
   }

  }
}
公共类ImageAdapter扩展了BaseAdapter{
ArrayList数据=null;
公共字符串[]myRemoteImages;
私人语境;
公共ImageAdapter(上下文、ArrayList数据){
this.mContext=上下文;
这个数据=数据;
myRemoteImages=新字符串[this.data.size()];

对于(inti=0;iHi-Waqas,我对Java和Android还是比较陌生的。我如何使用“数据”来获取值?你能给我看一下代码吗?我尝试了一些方法,但仍然没有弄清楚。例如,
public String[]myRemoteImages={data.toString()}
Thanks我已经更新了答案。现在它展示了如何创建一个非常Waqas的路径图数组!!顺便说一句,imgList.size()和imgList.get(i)imgList无法解析。我正在使用data.size()和data.get(i)来代替。对吗?哦,是的,对不起,我的错误。我查看了你的代码来更新我的答案。我现在已经在我的答案中修复了:)再次感谢你的帮助。
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();
   }

  }
}