Android Open Source - SimpleJsonAdapter Advance Json Adapter Demo






From Project

Back to project page SimpleJsonAdapter.

License

The source code is released under:

Apache License

If you think the Android project SimpleJsonAdapter listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package os.ransj.demo;
/*from  w w  w. java  2s. c o  m*/
import android.app.ListActivity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.LruCache;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

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

import os.ransj.adapter.R;
import os.ransj.adapter.SimpleJsonAdapter;

/**
 * Created by ransj on 14/12/5.
 */
public class AdvanceJsonAdapterDemo extends ListActivity {
    private static final String KEY_IMG_URL = "k_img_url";
    private static final String KEY_IMG_TITLE = "k_img_title";
    private RequestQueue mQueue;
    private ImageLoader mImageLoader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("Beauties");
        mQueue = Volley.newRequestQueue(this);
        mImageLoader = new ImageLoader(mQueue, new BitmapLruCache());
        SimpleJsonAdapter adapter = new SimpleJsonAdapter(this, getData(), R.layout.item_images, new String[]{KEY_IMG_URL, KEY_IMG_TITLE}, new int[]{R.id.item_images_img, R.id.item_images_title});
        adapter.setViewBinder(new SimpleJsonAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, Object data, String textRepresentation) {
                if (view.getId() == R.id.item_images_img) {
                    ImageLoader.ImageListener listener = ImageLoader.getImageListener((ImageView) view,R.drawable.ic_launcher, R.drawable.ic_launcher);
                    mImageLoader.get(textRepresentation, listener);
                } else if (view.getId() == R.id.item_images_title) {
                    ((TextView)view).setText(textRepresentation);
                }
                return true;
            }
        });
        setListAdapter(adapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mQueue.cancelAll(this);
    }

    private JSONArray getData() {
        JSONArray array = new JSONArray();
        try {
            JSONObject one = new JSONObject();
            one.put(KEY_IMG_TITLE, "?????????????");
            one.put(KEY_IMG_URL, "http://www.jydoc.com/uploads/jydoc/p03501/200903090037094233.jpg");
            array.put(one);
            JSONObject two = new JSONObject();
            two.put(KEY_IMG_TITLE, "???????????");
            two.put(KEY_IMG_URL, "http://www.jydoc.com/uploads/jydoc/p03501/200903090037094234.jpg");
            array.put(two);
            JSONObject three = new JSONObject();
            three.put(KEY_IMG_TITLE, "???????????");
            three.put(KEY_IMG_URL, "http://www.jydoc.com/uploads/jydoc/p03501/200903090037094235.jpg");
            array.put(three);
            JSONObject four = new JSONObject();
            four.put(KEY_IMG_TITLE, "??????????");
            four.put(KEY_IMG_URL, "http://www.jydoc.com/uploads/jydoc/p03501/200903090037094236.jpg");
            array.put(four);
            JSONObject five = new JSONObject();
            five.put(KEY_IMG_TITLE, "?????????????");
            five.put(KEY_IMG_URL, "http://www.jydoc.com/uploads/jydoc/p03501/200903090037094237.jpg");
            array.put(five);
            JSONObject six = new JSONObject();
            six.put(KEY_IMG_TITLE, "??????????");
            six.put(KEY_IMG_URL, "http://www.jydoc.com/uploads/jydoc/p03501/200903090037094238.jpg");
            array.put(six);
            JSONObject seven = new JSONObject();
            seven.put(KEY_IMG_TITLE, "???????????");
            seven.put(KEY_IMG_URL, "http://www.jydoc.com/uploads/jydoc/p03501/200903090037094239.jpg");
            array.put(seven);
            JSONObject eight = new JSONObject();
            eight.put(KEY_IMG_TITLE, "??????????");
            eight.put(KEY_IMG_URL, "http://www.jydoc.com/uploads/jydoc/p03501/200903090037094240.jpg");
            array.put(eight);
            JSONObject nine = new JSONObject();
            nine.put(KEY_IMG_TITLE, "????????");
            nine.put(KEY_IMG_URL, "http://www.jydoc.com/uploads/jydoc/p03501/200903090037094241.jpg");
            array.put(nine);
            JSONObject ten = new JSONObject();
            ten.put(KEY_IMG_TITLE, "????????");
            ten.put(KEY_IMG_URL, "http://www.jydoc.com/uploads/jydoc/p03501/200903090037094242.jpg");
            array.put(ten);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return array;
    }

    public class BitmapLruCache implements ImageLoader.ImageCache {

        private LruCache<String, Bitmap> mCache;

        public BitmapLruCache() {
            int maxSize = 10 * 1024 * 1024;
            mCache = new LruCache<String, Bitmap>(maxSize) {
                @Override
                protected int sizeOf(String key, Bitmap value) {
                    return value.getRowBytes() * value.getHeight();
                }
            };
        }

        @Override
        public Bitmap getBitmap(String url) {
            return mCache.get(url);
        }

        @Override
        public void putBitmap(String url, Bitmap bitmap) {
            mCache.put(url, bitmap);
        }

    }
}




Java Source Code List

com.android.volley.AuthFailureError.java
com.android.volley.CacheDispatcher.java
com.android.volley.Cache.java
com.android.volley.DefaultRetryPolicy.java
com.android.volley.ExecutorDelivery.java
com.android.volley.NetworkDispatcher.java
com.android.volley.NetworkError.java
com.android.volley.NetworkResponse.java
com.android.volley.Network.java
com.android.volley.NoConnectionError.java
com.android.volley.ParseError.java
com.android.volley.RequestQueue.java
com.android.volley.RequestTest.java
com.android.volley.Request.java
com.android.volley.ResponseDelivery.java
com.android.volley.Response.java
com.android.volley.RetryPolicy.java
com.android.volley.ServerError.java
com.android.volley.TimeoutError.java
com.android.volley.VolleyError.java
com.android.volley.VolleyLog.java
com.android.volley.mock.MockCache.java
com.android.volley.mock.MockHttpClient.java
com.android.volley.mock.MockHttpStack.java
com.android.volley.mock.MockHttpURLConnection.java
com.android.volley.mock.MockNetwork.java
com.android.volley.mock.MockRequest.java
com.android.volley.mock.MockResponseDelivery.java
com.android.volley.mock.TestRequest.java
com.android.volley.mock.WaitableQueue.java
com.android.volley.toolbox.AndroidAuthenticator.java
com.android.volley.toolbox.Authenticator.java
com.android.volley.toolbox.BasicNetwork.java
com.android.volley.toolbox.ByteArrayPool.java
com.android.volley.toolbox.ClearCacheRequest.java
com.android.volley.toolbox.DiskBasedCache.java
com.android.volley.toolbox.HttpClientStack.java
com.android.volley.toolbox.HttpHeaderParser.java
com.android.volley.toolbox.HttpStack.java
com.android.volley.toolbox.HurlStack.java
com.android.volley.toolbox.ImageLoader.java
com.android.volley.toolbox.ImageRequest.java
com.android.volley.toolbox.JsonArrayRequest.java
com.android.volley.toolbox.JsonObjectRequest.java
com.android.volley.toolbox.JsonRequest.java
com.android.volley.toolbox.NetworkImageView.java
com.android.volley.toolbox.NoCache.java
com.android.volley.toolbox.PoolingByteArrayOutputStream.java
com.android.volley.toolbox.RequestFuture.java
com.android.volley.toolbox.StringRequest.java
com.android.volley.toolbox.Volley.java
com.android.volley.utils.CacheTestUtils.java
com.android.volley.utils.ImmediateResponseDelivery.java
os.ransj.adapter.SimpleJsonAdapter.java
os.ransj.demo.AdvanceJsonAdapterDemo.java
os.ransj.demo.MainEntry.java
os.ransj.demo.SimpleJsonAdapterDemo.java