Android Open Source - Android-VolleySample Forecast Adapter






From Project

Back to project page Android-VolleySample.

License

The source code is released under:

MIT License

If you think the Android project Android-VolleySample 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 fr.alainmuller.volleysample.app.ui.adapter;
/*w w w.  j a va  2 s . c om*/
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.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;

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

import fr.alainmuller.volleysample.app.R;
import fr.alainmuller.volleysample.app.ui.activity.MainActivity;

/**
 * Classe reprsentant un Adapter pour chaque prvision mto de la liste rcupre
 * Created by Alain MULLER on 16/05/2014.
 */
public class ForecastAdapter extends BaseAdapter {

    private final Context mContext;
    private final ImageLoader mVolleyImageLoader;
    private JSONArray mForecastList;

    public ForecastAdapter(Context mContext, ImageLoader mVolleyImageLoader) {
        this.mContext = mContext;
        this.mVolleyImageLoader = mVolleyImageLoader;
    }

    public void updateForecast(JSONArray forecasts) {
        mForecastList = forecasts;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return (mForecastList == null) ? 0 : mForecastList.length();
    }

    @Override
    public JSONObject getItem(int position) {
        JSONObject item = null;
        if (mForecastList != null) {
            try {
                item = mForecastList.getJSONObject(position);
            } catch (JSONException e) {
                Log.e(this.getClass().getSimpleName(), "Exception JSON sur la rcupration de l'objet en [" + position + "] : " + e.getMessage());
            }
        }
        return item;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView forecast;
        NetworkImageView icon;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.forecast_list_item, parent, false);
            forecast = (TextView) convertView.findViewById(R.id.tv_forecast);
            icon = (NetworkImageView) convertView.findViewById(R.id.iv_icon);
            convertView.setTag(new ViewHolder(forecast, icon));
        } else {
            ViewHolder viewHolder = (ViewHolder) convertView.getTag();
            forecast = viewHolder.getForecast();
            icon = viewHolder.getIcon();
        }

        // On rcupre les donnes du JSONObject et on les insre dans les view
        JSONObject jsonObject = getItem(position);
        forecast.setText(jsonObject.optString(MainActivity.KEY_FORECAST).trim());
        icon.setImageUrl(jsonObject.optString(MainActivity.KEY_ICON_URL), mVolleyImageLoader);

        return convertView;
    }

    static final class ViewHolder {
        private final TextView mForecast;
        private final NetworkImageView mIcon;

        public ViewHolder(TextView forecast, NetworkImageView icon) {
            mForecast = forecast;
            mIcon = icon;
        }

        public TextView getForecast() {
            return mForecast;
        }

        public NetworkImageView getIcon() {
            return mIcon;
        }
    }
}




Java Source Code List

fr.alainmuller.volleysample.app.VolleySampleApplication.java
fr.alainmuller.volleysample.app.config.AppConfig.java
fr.alainmuller.volleysample.app.ui.activity.MainActivity.java
fr.alainmuller.volleysample.app.ui.adapter.ForecastAdapter.java
fr.alainmuller.volleysample.app.util.BitmapLruCache.java