将毕加索的图像添加到Gridview

时间:2016-03-28 14:32:49

标签: android gridview

我正在开发一个项目,我可以从数据库中动态获取图像URL。我之前在LinearLayout创建了一个画廊,这很好。但我想创建一个Gridview,我可以用同样的方式添加照片。任何人都可以给我提示或从哪里开始的想法。我还根据这里的教程创建了一个GridView片段(我想要片段中的gridview)和ImageAdapter类。
http://developer.android.com/guide/topics/ui/layout/gridview.html

本教程用于从本地资源加载我不需要的图像。 此外,我尝试在线搜索,但找不到合适的方法。任何帮助,将不胜感激。谢谢!。

编辑:

ImageAdapter.java

package com.example.imran.myapp;

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

import com.squareup.picasso.Picasso;

/**
 * Created by imran on 28-Mar-16.
 */
public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    String url = "https://www.google.com/images/srpr/logo11w.png";
    public ImageAdapter(Context c) {
        mContext = c;
    }

    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(200, 200));
        } else {
            imageView = (ImageView) convertView;
        }

        Picasso.with(this.mContext).load(url).resize(100, 100).into(imageView);
        Picasso.with(this.mContext).setLoggingEnabled(true);

        return imageView;
    }

    // references to our images
    public Integer[] mThumbIds = {
          /*R.drawable.title_background,
            R.drawable.title_background,
            R.drawable.title_background,  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
            */
    };
}

gridgallery.java

package com.example.imran.myapp;


import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.squareup.picasso.Picasso;

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

import java.util.HashMap;
import java.util.Map;


/**
 * A simple {@link Fragment} subclass.
 */
public class gridgallery extends Fragment {


    public gridgallery() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gridgallery, null);
        Myserver myserver = new Myserver();

        postStringRequest(myserver.url+"/api/albums/getalbums.php",view);
        return view;

        // Inflate the layout for this fragment
        //return inflater.inflate(R.layout.fragment_gridgallery, container, false);
    }

    public View postStringRequest(final String url,final View view){
        //final View view = inflater.inflate(R.layout.fragment_gallery, null);
        final TextView t = (TextView)view.findViewById(R.id.main_msg_gallery);
        // Instantiate the RequestQueue.
        RequestQueue queue = Volley.newRequestQueue(getContext());

        // Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Display the first 500 characters of the response string

                       // LinearLayout linearLayout = (LinearLayout)view.findViewById(R.id.fragment_gallery);
                        //GridView gridView = (GridView)view.findViewById(R.id.fragment_gridgallery);
                        //gridView.setAdapter(new ImageAdapter(getContext()));
                        try {
                            JSONArray jsonObj = new JSONArray(response);
                            Myserver myserver = new Myserver();
                            for (int i=0;i<jsonObj.length();i++){
                                JSONObject c = jsonObj.getJSONObject(i);

                                //ImageView albumpic = new ImageView(getContext());
                                //Picasso.with(getContext()).load(myserver.url+"/images/thumbs/tn_"+c.getString("album_thumbnail")).centerCrop().resize(200,200).into(albumpic);
                                //linearLayout.addView(albumpic);
                                // gridView.addView(albumpic);

                                GridView gridView = (GridView) view.findViewById(R.id.fragment_gridgallery);
                                ImageAdapter myadap = new ImageAdapter(getContext());
                                myadap.url = myserver.url+"/images/thumbs/tn_"+c.getString("album_thumbnail");
                                gridView.setAdapter(myadap);

                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                t.setText("Server error - Unable to reach server");
                Toast.makeText(getContext(), "Unable to reach server", Toast.LENGTH_LONG).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("getalbum", "getalbum");
                return params;
            }
        };
        queue.add(stringRequest);
        return view;
    }



}

2 个答案:

答案 0 :(得分:1)

这是我最近的工作。有很多方法可以做到这一点,但是你总是需要扩展/导入ListAdapter,它是listViews所有类型的基础。以下是方法:

  1. 创建一个适配器,就像制作扩展ListAdapter的ListView或list-array时一样。 (对我来说,我称之为GroupTileAdapater.java)
  2. 您必须为磁贴创建自己的视图,就像您为每个列表创建的一样。对我来说,我只是创建了一个布局并用imageview填充整个视图,你可以自定义你想要的任何内容。(在res / layout中称为media_tile.xml)
  3. 然后,您可以通过查找在平铺图像视图中实例化的相应ID来指定适配器内的图像。 (通过调用毕加索的方法,您可以查看我最近发布的问题,Load image through picasso
  4. 更具体地说,你将覆盖listAdapter,getCount,getItem,getItemId,getView中的4个方法,并且在设置适配器时,你会将图像列表传递给构造函数,而getView中的内容就是你所在的位置显示图像。

    1. 现在,您只需将适配器设置为您所处的任何片段/活动,无论您初始化gridview的布局如何。
    2. 我不知道您的网址类型是什么,但如果它以文件/内容开头,请查看以下内容contentUri:show image,并与picasso合并。有youtube视频可以帮助您了解更多有关listAdapter的信息。希望它有所帮助!

答案 1 :(得分:0)

谢谢你们的帮助,我终于明白了。

public class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context c, String urls[]) { mContext = c; mThumbIds = urls; //myurls = urls; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return position; //return 0; } public String getURL(int position){ return mThumbIds[position]; } // 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(200, 200)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(20, 20, 20, 20); } else { imageView = (ImageView) convertView; } // imageView.setImageResource(mThumbIds[position]); Picasso.with(mContext) .load(mThumbIds[position]) .into(imageView); return imageView; } //private ArrayList<String> myurls = new ArrayList<String>(); // references to our images private String[] mThumbIds = { }; }

gridgallery.java

package com.example.imran.myapp; public class gridgallery extends Fragment { public gridgallery() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_gridgallery, null); Myserver myserver = new Myserver(); postStringRequest(myserver.url + "/api/albums/getalbums.php", view); return view; } public View postStringRequest(final String url,final View view){ //final View view = inflater.inflate(R.layout.fragment_gallery, null); // Instantiate the RequestQueue. RequestQueue queue = Volley.newRequestQueue(getContext()); // Request a string response from the provided URL. StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { GridView gv = (GridView)view.findViewById(R.id.fragment_gridgallery); ArrayList<String> urls2 = new ArrayList<String>(); try { JSONArray jsonObj = new JSONArray(response); Myserver myserver = new Myserver(); for (int i=0;i<jsonObj.length();i++){ JSONObject c = jsonObj.getJSONObject(i); String imgurl = myserver.url+"/images/thumbs/tn_"+c.getString("album_thumbnail"); urls2.add(imgurl); } String myabc[] = urls2.toArray(new String[urls2.size()]); final ImageAdapter myadapter = new ImageAdapter(getContext(),myabc); gv.setAdapter(myadapter); gv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getContext(),myadapter.getURL(position),Toast.LENGTH_LONG).show(); } }); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getContext(), "Unable to reach server", Toast.LENGTH_LONG).show(); } }) { @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("getalbum", "getalbum"); return params; } }; queue.add(stringRequest); return view; } } (片段)

styles.xml