I am working on a project in which I get images URL dynamically from database. I previous created a gallery in LinearLayout
which is fine. But I want to create a Gridview
in which i can add the photos in same way. Can anyone give me hint or idea of from where to start. I have also created a GridView
fragment (I want gridview in fragment) and ImageAdapter
class according to tutorial here.
http://developer.android.com/guide/topics/ui/layout/gridview.html
This tutorial is for loading images from local resources which I don't need. Also I have tried searching online but couldn't find an appropriate way. Any help would be appreciated. Thank You!.
Edit:
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;
}
}