2011-07-25 10 views
0

週末に多くの進歩を遂げた後、私はgridViewとImageAdapterに苦しんでいます。今まで私が行ってきたことはすべて、私の主なアクティビティでユーザーが選択したタグを取得し、クエリを実行してjson_ecodedデータを返します。 jsonデータをバンドルとインテントを介してshowThumbアクティビティに渡します。このアクティビティでは、ImageAdapterをグリッドビューにアタッチします。しかし、ImageAdapterにjsonのデータを渡してから、jsonのデータを使用してサムネイルにパスを渡す方法はわかりません。ImageAdapterにJSONデータを渡す別のクラスから画像パスを提供する

アドバイスがあれば、驚くほど助けになることができます。

JSONデータ:

{"id":["1","2","3"],"name":["Dragon","Butterfly","Tattoo"],"thumb":["thm_polaroid.jpg","thm_default.jpg","thm_enhanced-buzz-9667-1270841394-4.jpg"],"path":["polaroid.jpg","default.jpg","enhanced-buzz-9667-1270841394-4.jpg"]} 

ShowThumbクラス

package com.flash_Images; 

import android.os.Bundle; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.GridView; 
import java.util.ArrayList; 
import java.util.List; 

import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.content.ActivityNotFoundException; 
import android.content.Intent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.*; 

public class showThumb extends Activity{ 



    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.gridlayout); 

      Bundle bundle = getIntent().getExtras(); 
     String jsonData = bundle.getString("jsonData"); 

     try { 
      JSONObject jsonObj = new JSONObject(jsonData); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     GridView gridview = (GridView) findViewById(R.id.gridview); 
     gridview.setAdapter(new ImageAdapter(this)); 

     gridview.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
       Toast.makeText(showThumb.this, "" + position, Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 
} 

ImageAdapterクラス:

package com.flash_Images; 

import java.util.ArrayList; 

import org.json.JSONArray; 
import org.json.JSONObject; 

import android.content.Context; 
import android.database.DataSetObserver; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.widget.ListAdapter; 

public class ImageAdapter extends BaseAdapter { 
    private Context mContext; 

    private JSONObject jsonObj; 

    public ImageAdapter(Context c) { 
     mContext = c; 
    } 

    public ImageAdapter(showThumb c) { 
     // TODO Auto-generated constructor stub 
    } 

    public int getCount() { 
     return mThumbIds.length; 
    } 

    public Object getItem(int position) { 
     return null; 
    } 

    public long getItemId(int position) { 
     return 0; 
    } 

    // create a new ImageView for each item referenced by the Adapter 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView; 
     if (convertView == null) { // if it's not recycled, initialize some attributes 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(8, 8, 8, 8); 
     } else { 
      imageView = (ImageView) convertView; 
     } 

     imageView.setImageResource(mThumbIds[position]); 
     return imageView; 
    } 


    JSONArray menuitemArr = popupObject.getJSONArray("thumb"); 


    // references to our images 
    private Integer[] mThumbIds = { 


//I need to loop through my json "thumb:" items below to add the image paths for the grid view 

      for (int i = 0; i < menuitemArr.length(); i++) { 
       // printing the values to the logcat 
       setImageDrawable(Drawable.createFromPath(myFooArray[position].thumb); 
      } 

      //This is the example data I need to replace 
      /** R.drawable.sample_2, R.drawable.sample_3, 
      R.drawable.sample_4, R.drawable.sample_5, 
      R.drawable.sample_6, R.drawable.sample_7, 
      R.drawable.sample_0, R.drawable.sample_1, 
      R.drawable.sample_2, R.drawable.sample_3, 
      R.drawable.sample_4, R.drawable.sample_5, 
      R.drawable.sample_6, R.drawable.sample_7, 
      R.drawable.sample_0, R.drawable.sample_1, 
      R.drawable.sample_2, R.drawable.sample_3, 
      R.drawable.sample_4, R.drawable.sample_5, 
      R.drawable.sample_6, R.drawable.sample_7*/ 
    }; 
} 

答えて

1

物事を動作させるために代わりBaseAdapterArrayAdapterを使用してください。

menuitemArrのクラスを、jsonパーサーから返されたデータで構成できるようにします。

あなたは

ArrayAdapterhereの例を見てみましょ解析されたデータのリストを渡すことができますコンストラクタを作成ArrayAdapter

このような

public class ImageAdapter extends ArrayAdepter<YOUNEWCLASS>

を定義します。

さて、これはあなたを助けることができるこの

GridView gridview = (GridView) findViewById(R.id.gridview); 
     gridview.setAdapter(new ImageAdapter(this,listofYourNewCalss)); 

希望のようなアダプタを使用します。

+0

おかげさまで+1 – Denoteone

関連する問題