将JSON数据传递给我的ImageAdapter从另一个类传递我的图像路径

时间:2011-07-25 05:32:48

标签: java android json

好的,所以在周末取得了很多进展后,我陷入了gridView和ImageAdapter的困境。到目前为止,我所做的只是我的主要活动获取用户选择的标签,执行查询并返回json_ecoded数据。我通过bundle和intent传递json数据到我的showThumb活动。此活动应该将ImageAdapter附加到我的网格视图。但我无法弄清楚如何将我的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 Class

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 个答案:

答案 0 :(得分:1)

使用ArrayAdapter代替BaseAdapter来完成工作。

创建一个menuitemArr类,以便它们可以包含从json解析器返回的数据。

像这样定义ArrayAdapter

public class ImageAdapter extends ArrayAdepter<YOUNEWCLASS>

创建一个构造函数,您可以在其中传递已解析数据的列表

查看ArrayAdapter here

的示例

现在使用像这样的适配器

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

希望这可以帮到你。