2012-03-04 6 views
0

私はpictureActivity.java配列を別のクラスimageAdapter.javaに渡したいと思います。 トーストでimgPathの結果を取得しますが、文字列を別のクラスに移動する方法がわかりません。Arraylistの文字列を別のクラスのアダプタに渡すには?

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に配列文字列を入れたかったです。お使いのアダプタでのArrayListを取得した後

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

    } 
} 

((Gallery) findViewById(R.id.gallery)) 
      .setAdapter(new ImageAdapter(this, imgList)); 

とあなたのアダプタクラスで

は次のようにコンストラクタを作成します。あなたは、アダプタクラスのオブジェクトを初期化しながら、

答えて

1

あなたは、アレイアダプタを渡すことができますクラスから、あなたが望むあらゆる値をフェッチすることができます。

+0

こんにちはWaqas、私はまだJavaとAndroidの比較的新しいです。 値を取得するには、どのように "データ"を再生しますか?私にコードを表示できますか? 私はいくつかの方法を試みたが、私はまだそれを理解することはできません。例: 'public String [] myRemoteImages = {data.toString()};' ありがとう – Eric

+0

私は答えを更新しました。今すぐパスの配列を作成する方法を示します – waqaslam

+0

ありがとうWaqas !! Btw imgList.size()&imgList.get(i)imgListを解決できません。 代わりにdata.size()とdata.get(i)を使用しています。 いいですか? – Eric

関連する問題