tegarkurniawan

Image3

Jun 14th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 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.  
  87.  
  88.  
  89.  
  90. imagename = (ImageView) rowView.findViewById(R.id.icon);
  91.  
  92.  
  93. new LoadImage().execute(new Gson().toJson(icon[position]).replaceAll("^\"|\"$", ""));
  94.  
  95. String nama = new Gson().toJson(name[position]).replaceAll("^\"|\"$", "");
  96. name2.setText(nama);
  97. Log.i("Tag", "Name data5 " + new Gson().toJson(icon[position]).replaceAll("^\"|\"$", ""));
  98.  
  99.  
  100. Log.i("Tag", "Name data3 " + new Gson().toJson(name));
  101.  
  102.  
  103. return rowView;
  104. };
  105.  
  106. private class LoadImage extends AsyncTask<String, String, Bitmap> {
  107. @Override
  108. protected void onPreExecute() {
  109. super.onPreExecute();
  110.  
  111.  
  112. }
  113. protected Bitmap doInBackground(String... args) {
  114. try {
  115. bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
  116.  
  117. } catch (Exception e) {
  118. e.printStackTrace();
  119. }
  120. return bitmap;
  121. }
  122.  
  123. protected void onPostExecute(Bitmap image) {
  124.  
  125. if(image != null){
  126. imagename.setImageBitmap(image);
  127.  
  128.  
  129. }else{
  130.  
  131.  
  132.  
  133. }
  134. }
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment