Android JSON Adapter

I am taking a break from server side development for a few days and doing some android development. So its back to java *sigh* and so simple things seem much harder. I was surprised to find that Android doesn’t have an adapter for json given that so many web apis provide this format. I considered violating DRY, recreating my Django models in java and writing some conversion code, but I am going to see how I get on leaving all the model definitions to Django.

To this end, I created this naive JSON adapter based on the build-in ArrayAdapter and SimpleCursorAdapter.



package ie.shizzle;

import ie.shizzle.util.Utils;

import java.util.Arrays;

import org.json.JSONObject;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Simple Adapter for JSON
 * 
 * Based on {@link ArrayAdapter} and {@link CursorAdapter}
 * 
 * @author dave
 *
 */
public class JSONArrayAdapter extends ArrayAdapter {
	private int mResource;
	private LayoutInflater mInflater;
	protected String[] mFrom;
    protected int[] mTo;
    
	public JSONArrayAdapter(Context context, int textViewResourceId, JSONObject[] objects, String[] from, int[]to) {
        super(context, textViewResourceId, 0, Arrays.asList(objects));
		mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mResource = textViewResourceId;
        mFrom = from;
        mTo = to;
    }

	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view;
        if (convertView == null) {
            view = mInflater.inflate(mResource, parent, false);
        } else {
            view = convertView;
        }		
        JSONObject item = (JSONObject) getItem(position);
        bindView(view, item);

        return view;
	}
	/**
	 * a la bindView of {@link CursorAdapter}
	 * TODO allow for more complex bindings
	 */
	public void bindView(View view, JSONObject json) {
        final int count = mTo.length;
        final String[] from = mFrom;
        final int[] to = mTo;

        for (int i = 0; i < count; i++) {
            final View v = view.findViewById(to[i]);
            if (v != null) {
                    String text = "";
					text = Utils.getText(json,from[i]);

                    if (v instanceof TextView) {
                        setViewText((TextView) v, text);
                    } else if (v instanceof ImageView) {
                        setViewImage((ImageView) v, text);
                    } else {
                        throw new IllegalStateException(v.getClass().getName() + " is not a " +
                                " view that can be bound by this JSONArrayAdapter");
                    }
            }
        }
    }
	
	
	public void setViewImage(ImageView v, String value) {
        try {
            v.setImageResource(Integer.parseInt(value));
        } catch (NumberFormatException nfe) {
            v.setImageURI(Uri.parse(value));
        }
    }
	
	public void setViewText(TextView v, String text) {
        v.setText(text);
    }

	
}
Advertisements

About bebblebrox

I am an experienced software developer with over five year’s commercial experience with strong software design and development skills. I am searching for an ambitious start-up company with a shared passion for delivering quality software products.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s