Skip to content

Instantly share code, notes, and snippets.

@vjdhama
Last active August 29, 2015 13:56
Show Gist options
  • Save vjdhama/9239796 to your computer and use it in GitHub Desktop.
Save vjdhama/9239796 to your computer and use it in GitHub Desktop.
Volley class
public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageCache {
public BitmapLruCache(int maxSize) {
super(maxSize);
}
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight();
}
@Override
public Bitmap getBitmap(String url) {
return get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
put(url, bitmap);
}
}
public class CustomAdapter extends ArrayAdapter<Offer> {
// View lookup cache
public CustomAdapter(Context context,
int textViewResourceId,
ArrayList<Offer> offers
){
super(context,textViewResourceId,offers);
}
private class ViewHolder {
TextView briefOfferTextView;
TextView extendedOfferTextView;
NetworkImageView offerImageView;
public ViewHolder(View v) {
briefOfferTextView = (TextView) v.findViewById(R.id.briefOfferTextView);
extendedOfferTextView = (TextView) v.findViewById(R.id.extendedOfferTextView);
offerImageView = (NetworkImageView) v.findViewById(R.id.offerImageView);
v.setTag(this);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.offer_each_item, null);
}
ViewHolder holder = (ViewHolder) v.getTag();
if (holder == null) {
holder = new ViewHolder(v);
v.setTag(holder);
}
Offer offer = getItem(position);
holder.offerImageView.setImageUrl(offer.getImageUrl(), MyVolley.getInstance(getContext()).getImageLoader());
holder.briefOfferTextView.setText(offer.getOfferTitle());
holder.extendedOfferTextView.setText(offer.getOfferDesc());
return v;
}
}
public class MainActivity extends Activity {
private boolean mHasData = false;
private boolean mInError = false;
TextView headerTextView;
ListView offersListView;
final String URL = "http://172.20.1.246:3000/api/offer.json?param=new";
private ArrayList<Offer> mOffers = new ArrayList<Offer>();
public static CustomAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.offer_list);
headerTextView = (TextView) findViewById(R.id.headerTextView);
offersListView = (ListView) findViewById(R.id.offersListView);
headerTextView.setText("NEW OFFERS");
mAdapter = new CustomAdapter(this, 0, mOffers);
//adapter.addAll(arrayOfOffers);
offersListView.setAdapter(mAdapter);
}
@Override
protected void onResume() {
super.onResume();
if (!mHasData && !mInError) {
loadPage();
}
}
private void loadPage() {
RequestQueue queue = MyVolley.getInstance(getApplicationContext()).getRequestQueue();
JsonObjectRequest myReq = new JsonObjectRequest(URL,
null,
createMyReqSuccessListener(),
createMyReqErrorListener());
queue.add(myReq);
}
private Response.Listener<JSONObject> createMyReqSuccessListener() {
return new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray offers = response.getJSONArray("offers");
JSONObject offer;
for (int i = 0; i < offers.length(); i++) {
offer = offers.getJSONObject(i);
mOffers.add(new Offer(offer.getString("des"), offer.getString("des"),offer.getString("iu")));
}
mAdapter.notifyDataSetChanged();
} catch (JSONException e) {
showErrorDialog(e.getMessage());
}
}
};
}
private Response.ErrorListener createMyReqErrorListener() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
showErrorDialog(error.getMessage());
}
};
}
private void showErrorDialog(String error) {
mInError = true;
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setMessage("Error occured : " + error);
b.show();
}
public void onListItemClick(ListView parent, View v, int position, long id){
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
public class MyVolley {
private static MyVolley mInstance = null;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;
private MyVolley(Context context){
mRequestQueue = Volley.newRequestQueue(context);
mImageLoader = new ImageLoader(this.mRequestQueue, new BitmapLruCache(cacheSize));
}
public static MyVolley getInstance(Context context){
if(mInstance == null){
mInstance = new MyVolley(context);
}
return mInstance;
}
public RequestQueue getRequestQueue(){
return this.mRequestQueue;
}
public ImageLoader getImageLoader(){
return this.mImageLoader;
}
}
public class Offer {
String offerTitle;
String offerDesc;
String imageUrl;
private String errorImageUrl;
public Offer(String oT,String oD, String iu){
super();
errorImageUrl = "Not to see.jpg";
offerTitle = oT;
offerDesc = oD;
if (iu.equals(null)){
iu = errorImageUrl;
}else
imageUrl = iu;
}
public String getImageUrl(){
return imageUrl;
}
public String getOfferTitle() {
return offerTitle;
}
public String getOfferDesc() {
return offerDesc;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment