tegarkurniawan

image4

Jun 14th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. package com.tulpar.restapi.restapi;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.content.ClipData;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.os.AsyncTask;
  9. import android.util.Log;
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.widget.ArrayAdapter;
  14. import android.widget.ImageView;
  15. import android.widget.TextView;
  16.  
  17. import com.google.gson.Gson;
  18. import com.tulpar.restapi.restapi.models.Service;
  19.  
  20. import java.io.BufferedInputStream;
  21. import java.io.BufferedOutputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.net.HttpURLConnection;
  26. import java.net.MalformedURLException;
  27. import java.net.URL;
  28. import java.net.URLConnection;
  29. import java.util.List;
  30.  
  31. /**
  32. * Created by tegar on 14/06/16.
  33. */
  34. public class ServiceAdapter extends ArrayAdapter<String> {
  35.  
  36. private final Activity context;
  37. private final String[] name;
  38. private final String[] icon;
  39. private List<Service> service;
  40. private int sizes;
  41. Bitmap bitmap;
  42.  
  43. ImageView imagename;
  44.  
  45. public ServiceAdapter(Activity context,int size , List<Service> services) {
  46. super(context, R.layout.content_main);
  47. this.context=context;
  48. this.name = new String[services.size()];
  49. this.icon = new String[services.size()];
  50. sizes = size;
  51. this.service = services;
  52. for (int i = 0; i < services.size(); i++) {
  53.  
  54. name[i] = services.get(i).getName();
  55. icon[i] = services.get(i).getIcon();
  56. }
  57.  
  58. Log.i("Tag", "Name data " + new Gson().toJson(services));
  59. Log.i("Tag", "Name data Name " + new Gson().toJson(name));
  60. }
  61.  
  62. @Override
  63. public int getCount() {
  64.  
  65. return service.size();
  66.  
  67. }
  68.  
  69. @Override
  70. public long getItemId(int position) {
  71.  
  72. return position;
  73. }
  74.  
  75. public View getView(int position,View view,ViewGroup parent) {
  76. LayoutInflater inflater=context.getLayoutInflater();
  77. View rowView=inflater.inflate(R.layout.content_main, null, true);
  78. Log.i("Tag", "Name data Size2 " + new Gson().toJson(position));
  79. sizes = position;
  80.  
  81.  
  82. name[position] = service.get(position).getName();
  83. icon[position] = service.get(position).getIcon();
  84. TextView name2 = (TextView) rowView.findViewById(R.id.name);
  85.  
  86. imagename = (ImageView) rowView.findViewById(R.id.icon);
  87.  
  88.  
  89. new LoadImage().execute(icon[position]);
  90.  
  91. String nama = name[position];
  92. name2.setText(nama);
  93.  
  94.  
  95.  
  96. return rowView;
  97. };
  98.  
  99. private class LoadImage extends AsyncTask<String, String, Bitmap> {
  100. @Override
  101. protected void onPreExecute() {
  102. super.onPreExecute();
  103.  
  104.  
  105. }
  106. protected Bitmap doInBackground(String... args) {
  107. try {
  108. bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
  109.  
  110. } catch (Exception e) {
  111. e.printStackTrace();
  112. }
  113. return bitmap;
  114. }
  115.  
  116. protected void onPostExecute(Bitmap image) {
  117.  
  118. if(image != null){
  119. imagename.setImageBitmap(image);
  120.  
  121.  
  122. }else{
  123.  
  124.  
  125.  
  126. }
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment