如何从Android中的gridview发送Another Activity中的所有图像?

时间:2014-10-29 06:29:33

标签: android

我想通过意图从Adapter类将图像发送到另一个Activity并在该Activity上逐个滑动。我试过但没有在其他Activity中获取图像。

这是代码。

public class GridViewAdapter extends BaseAdapter
{
    private Context context;
    public ArrayList<HashMap<String,String>> mThumbIds=new ArrayList<HashMap<String, String>>();
    AQuery aQuery;
    private LayoutInflater mInflater;

    public GridViewAdapter (Context context,ArrayList<HashMap<String,String>> data )
    {
        this.context= context;
        mThumbIds= data;
        aQuery=new AQuery(context);
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mThumbIds.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return  mThumbIds;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        // TODO Auto-generated method stub

        final ViewHolder holder;
        if (convertView == null)
        {
            convertView = mInflater.inflate(R.layout.photo_gallery_list_item, null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
            convertView.setTag(holder);
        }
        else 
        {
            holder = (ViewHolder) convertView.getTag();
        }

        aQuery.id(holder.imageView).image(mThumbIds.get(position).get("flag"),true,true);


        convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent i = new Intent(context, Display.class);
                i.putExtra("pics",mThumbIds.get(position));
                context.startActivity(i);

            }
        });
        return convertView;

    }

    class ViewHolder 
    {

        ImageView imageView;
    }
  }

另一项活动

public class Display extends Activity
{
    ImageView imgview;

    public void onCreat(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.picture_display);
        imgview = (ImageView)findViewById(R.id.imageView_Grid);
        Intent i = getIntent();
        int[] xyz = i.getIntArrayExtra("pic");
        System.out.println("int[] xyz = " + xyz);

    }

}

JSON解析代码

public class Photo_Gallery extends Activity
{
    JSONObject jsonobject;
    JSONArray jsonarray;
    GridView gridview;
    GridViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    JSONParser jsonParser = new JSONParser();
    ProgressDialog pDialog;
    private String URL_PHOTO_GALLERY = "http://180.149.241.26:8181/timesofindia/photo_gallery.php";

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo_gallery);
        new DownloadJSON().execute();

        gridview = (GridView) findViewById(R.id.photoGallery);

        gridview.setOnItemClickListener(new OnItemClickListener() {
          @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {
                // TODO Auto-generated method stub
                // Sending image id to FullScreenActivity
                Intent i = new Intent(getApplicationContext(), Display.class);
                // passing array index
                i.putExtra("id", position);
                startActivity(i);
            }
        });

    }

     private class DownloadJSON extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            mProgressDialog = new ProgressDialog(Photo_Gallery.this);
            mProgressDialog.setTitle("Wait");
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) 
        {

            arraylist = new ArrayList<HashMap<String, String>>();
            jsonobject = JSONfunctions.getJSONfromURL(URL_PHOTO_GALLERY);
            System.out.println("Json String = " + jsonobject.toString());


            try
            {
                jsonarray = jsonobject.getJSONArray("photo_gallary");

                for (int i = 0; i < jsonarray.length(); i++)
                {
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    map.put("flag", jsonobject.getString("image"));
                    arraylist.add(map);
                    Log.e("arraylist","=" + arraylist.add(map) );
                }
            } 
            catch (JSONException e)
            {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args)
        {
            adapter = new GridViewAdapter(Photo_Gallery.this,arraylist);
            gridview.setAdapter(adapter);
            mProgressDialog.dismiss();
        }
    }
}

3 个答案:

答案 0 :(得分:0)

你必须使静态这个变量替换为:

public ArrayList<HashMap<String,String>> mThumbIds=newArrayList<HashMap<String, String();

public static ArrayList<HashMap<String,String>> mThumbIds=new ArrayList<HashMap<String, String>>();

并通过意图传递此变量,如:

i.putStringArrayListExtra(key,mThumbIds);

答案 1 :(得分:0)

首先,我认为不需要HashMap的ArrayList(因为你的适配器类只有一个ImageView而且不需要其他数据)。

public ArrayList<HashMap<String,String>> mThumbIds=new ArrayList<HashMap<String, String>>();

你可以简单地拥有一个StringList的StringList。像这样的东西。

public ArrayList<String> mThumbIds=new ArrayList<String>();

并将AsyncTask的doInBackground的try块中的代码更改为此

try
        {
            jsonarray = jsonobject.getJSONArray("photo_gallary");

            for (int i = 0; i < jsonarray.length(); i++)
            {

                jsonobject = jsonarray.getJSONObject(i);
                arraylist.add(jsonobject.getString("image"));

            }
        } 
        catch (JSONException e)
        {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

如果要将String String的整个ArrayList发送到另一个活动,那么您可以在适配器类中使用它。

    Intent i = new Intent(context, Display.class);
    i.putStringArrayListExtra("yourKey", mThumbIds);

并且在您的接收活动中,您可以像这样检索您的ArrayList

ArrayList<String> mThumbIds=getIntent().getStringArrayListExtra("yourKey");

答案 2 :(得分:0)

您需要从gridview中删除onItemClickListener,只需将该代码移到Adapter类中,如下所示:

    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent i = new Intent(context, Display.class);

            i.putStringArrayListExtra("pics", mThumbIds);
            i.putExtra("id", position);
            context.startActivity(i);

        }
    });

然后在你的Display acitivty onCreate()方法中获取额外内容并使用AQuery显示图像:

ImageView imgview;
AQuery aQuery;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.picture_display);
    aQuery = new AQuery(this);
    imgview = (ImageView)findViewById(R.id.imageView_Grid);

    Intent i = getIntent();
    ArrayList<String> mThumbIds = i.getStringArrayListExtra("pics");
    int id = i.getIntExtra("id", -1);

    aQuery.id(imgview).image(mThumbIds.get(id), true, true);
}