Joe Joe - 1 year ago 200
Java Question

i use Glide for my image display in android studio but how can i implement Glide to my listview?

I need help from anyone.. please respect my question..

ok, my problem is i want to use Glide for my listview but i dont know to do..

please constract my listviewadapter so that the Glide will work give me any other possible solution to achieve my goal..

my goal is i just want to display image and text in listview or gridview with Glide and JSON, the JSON result is from my php script..

here is my code..

package com.example.administrator.mosbeau;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ListView;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

* Created by Administrator on 9/18/2015.
public class CategoryFragment extends Fragment {

public static CategoryFragment newInstance(String id,String name) {
CategoryFragment fragment = new CategoryFragment();

Bundle bundle = new Bundle();
bundle.putString("id", id);
bundle.putString("name", name);

return fragment;

public CategoryFragment () {

EditText tpid, tpname;
String cid;
String cname;

String myJSON;
JSONObject jsonobject;
JSONArray jsonarray;
ListView productlistview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
public static String products_id = "products_id";
public static String products_name = "products_name";
public static String products_price = "products_price";
public static String products_image = "products_image";

Boolean InternetAvailable = false;
Seocnd detectconnection;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.categorylayout, container, false);


tpid = (EditText) rootView.findViewById(;
tpname = (EditText) rootView.findViewById(;

if(getArguments() != null) {
String catid = getArguments().getString("id");
String catname = getArguments().getString("name");

cid = catid;
cname = catname;

productlistview = (ListView) rootView.findViewById(;

//new DownloadJSON().execute();

detectconnection = new Seocnd(getActivity());
InternetAvailable = detectconnection.InternetConnecting();
if (InternetAvailable) {
} else {
NointernetFragment fragment = new NointernetFragment();
FragmentManager fragmentManager = getFragmentManager();
.replace(, fragment)

return rootView;

public void getProduct(){
class DownloadJSON extends AsyncTask<String, Void, String> {

protected void onPreExecute() {
// Create a progressdialog
mProgressDialog = new ProgressDialog(getActivity());
// Set progressdialog title
// Set progressdialog message
// Show progressdialog;

protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("");

// Depends on your web service
httppost.setHeader("Content-type", "application/json");

InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();

String line = null;
while ((line = reader.readLine()) != null)
sb.append(line + "\n");
result = sb.toString();
} catch (Exception e) {
// Oops
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
return result;

protected void onPostExecute(String result){

try {
// Locate the array name in JSON
JSONObject jsonObj = new JSONObject(myJSON);
jsonarray = jsonObj.getJSONArray("products");

arraylist = new ArrayList<HashMap<String, String>>();

for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject p = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("products_id", p.getString("products_id"));
map.put("products_name", p.getString("products_name"));
map.put("products_price", p.getString("products_price"));
map.put("products_image", p.getString("products_image"));
// Set the JSON Objects into the array
} catch (JSONException e) {
Log.e("Error", e.getMessage());

adapter = new ListViewAdapter(getActivity(), arraylist);
// Set the adapter to the ListView
// Close the progressdialog
DownloadJSON g = new DownloadJSON();

public void onAttach(Activity activity) {
((MainActivity) activity).onSectionAttached(2);

package com.example.administrator.mosbeau;

* Created by Administrator on 9/28/2015.

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

import com.bumptech.glide.Glide;

public class ListViewAdapter extends BaseAdapter {

// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();

public ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);

public int getCount() {
return data.size();

public Object getItem(int position) {
return null;

public long getItemId(int position) {
return 0;

public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView products_id;
TextView products_name;
TextView products_price;
ImageView products_image;

inflater = (LayoutInflater) context

View itemView = inflater.inflate(R.layout.product_listview_item, parent, false);
// Get the position
resultp = data.get(position);

// Locate the TextViews in product_listview_item.xml
products_id = (TextView) itemView.findViewById(;
products_name = (TextView) itemView.findViewById(;
products_price = (TextView) itemView.findViewById(;

// Locate the ImageView in product_listview_item.xml
products_image = (ImageView) itemView.findViewById(;

// Capture position and set results to the TextViews
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(resultp.get(CategoryFragment.products_image), products_image);
// Capture ListView item click
itemView.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
// Get the position
resultp = data.get(position);
Intent intent = new Intent(context, SingleItemView.class);
// Pass all data rank
intent.putExtra("products_id", resultp.get(CategoryFragment.products_id));
// Pass all data country
intent.putExtra("products_name", resultp.get(CategoryFragment.products_name));
// Pass all data population
// Pass all data flag
intent.putExtra("products_image", resultp.get(CategoryFragment.products_image));
// Start SingleItemView Class

return itemView;

Answer Source

Just use : Glide.with(context) .load(resultp.get(CategoryFragment.products_image)) .into(products_image);

in place of

imageLoader.DisplayImage(resultp.get(CategoryFragment.products_image), products_image);

in your

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download