I am trying to parse the images and text from url to list view.
I succesfully parse names but unable to parse Images. I follow these links....
I am getting bit map value null in image loader class..
I copied the MemoryCache and File Cache classes from the below links... Is there any mistake in my code?
My Json is: http://192.185.159.159/~charmmar/raj_spice/webservices/index.php?action=products
Is any problem for image parsing if json is large?
- http://www.androidbegin.com/tutorial/android-json-parse-images-and-texts-tutorial/
- http://luchfilip.wordpress.com/2014/02/25/android-how-to-parse-json-and-show-images-and-texts-in-a-listview/comment-page-1/
- http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/
in these links all contained ImageLoader
, FileCache
, MemoryCache
classes but I am unable to parse images... is any special library needed for this?
My class:
package com.example.dellizia;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.ListView;
public class RecipesJson extends Helper {
private String url = com.example.dellizia.Utility.urlpath + "products";
ArrayList<HashMap<String, String>> productList;
ProgressDialog pdialog;
final String TAG_DATA = "data";
final String TAG_ID = "id";
final static String TAG_NAME = "name";
final static String TAG_IMAGE = "images";
ListView listview;
RecipeJsonAdapter adapter;
SharedPreferences preferences;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
PrefEdit("pref__footer", "img_recipe");
footerBlock();
new DownloadJSON().execute();
}
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
pdialog = new ProgressDialog(RecipesJson.this);
// Set progressdialog title
// pdialog.setTitle("Android JSON Parse Tutorial");
// Set progressdialog message
pdialog.setMessage("Loading...");
pdialog.setIndeterminate(false);
// Show progressdialog
pdialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
productList = new ArrayList<HashMap<String, String>>();
try {
String res = JsonFunction.getJSONfromURL(url);
Log.e("","resvalue--------"+res);
JSONObject jsonobj = new JSONObject(res);
String id = jsonobj.getString("status");
Log.e("","id value in parse json--------"+id);
if (id.equals("1")) {
JSONArray userdata = jsonobj.getJSONArray("data");
for (int i = 0; i < userdata.length(); i++) {
HashMap<String, String> pdata = new HashMap<String, String>();
JSONObject obj = userdata.getJSONObject(i);
String catid = userdata.getJSONObject(i).getString("id");
String name = userdata.getJSONObject(i).getString("name");
String image = userdata.getJSONObject(i).getString("images");
pdata.put(TAG_ID, catid);
Log.e("", "TAG_ID---" + TAG_ID);
pdata.put(TAG_NAME, name);
Log.e("", "TAG_NAME---" + TAG_NAME);
Log.e("", "name---" + name);
pdata.put(TAG_IMAGE, image);
Log.e("", "TAG_IMAGE---" + TAG_IMAGE);
Log.e("", "image-----------" + image);
productList.add(pdata);
Log.e("", "product list----" + productList);
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.recipeslist);
// Pass the results into ListViewAdapter.java
adapter = new RecipeJsonAdapter(RecipesJson.this, productList);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
pdialog.dismiss();
}
}
}
My adapter class:
package com.example.dellizia;
import java.util.ArrayList;
import java.util.HashMap;
import com.example.androidhive.ImageLoader;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class RecipeJsonAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public RecipeJsonAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.recipeitems, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
//TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
//TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.recipeimage); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
Log.e("","names values ------------"+song.get(RecipesJson.TAG_NAME));
title.setText(song.get(RecipesJson.TAG_NAME));
//artist.setText(song.get(CustomizedListView.KEY_ARTIST));
// duration.setText(song.get(CustomizedListView.KEY_DURATION));
Log.e("","images values ------------"+song.get(RecipesJson.TAG_IMAGE));
imageLoader.DisplayImage(song.get(RecipesJson.TAG_IMAGE), thumb_image);
return vi;
}
}
This is my link for all classes.. http://ge.tt/8keNUh62?c. In this link all my used classes are available.
Please help!