tegarkurniawan

Adapter2

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