tegarkurniawan

recyclerviewAdapter

Jun 19th, 2016
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. package com.tulpar.restapi.restapi.adapter;
  2.  
  3. import android.app.Activity;
  4. import android.app.Fragment;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.support.v4.app.FragmentActivity;
  8. import android.support.v7.widget.RecyclerView;
  9. import android.util.Log;
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.widget.ImageView;
  14. import android.widget.TextView;
  15.  
  16. import com.google.gson.Gson;
  17. import com.squareup.picasso.Picasso;
  18. import com.tulpar.restapi.restapi.R;
  19. import com.tulpar.restapi.restapi.models.Detail;
  20. import com.tulpar.restapi.restapi.models.Nearby;
  21. import com.tulpar.restapi.restapi.models.Service;
  22.  
  23. import java.util.List;
  24.  
  25. /**
  26. * Created by tegar on 16/06/16.
  27. */
  28. public class NearbyAdapter extends RecyclerView.Adapter<NearbyAdapter.ViewHolder> {
  29. private static final String TAG = "CustomAdapter";
  30.  
  31. public final FragmentActivity activitys;
  32. public String[] brandname;
  33. public final String[] alamat;
  34. public final String[] icon;
  35. public final String[] distance;
  36. public final String[] status;
  37. public List<Nearby> nearby;
  38.  
  39.  
  40. public static class ViewHolder extends RecyclerView.ViewHolder {
  41. private final TextView textView, textView2, textView3;
  42. private final ImageView icon;
  43. public List<Nearby> nearbyy;
  44.  
  45. public ViewHolder(View v) {
  46. super(v);
  47.  
  48. v.setOnClickListener(new View.OnClickListener() {
  49. @Override
  50. public void onClick(View v) {
  51. Intent v3 = new Intent(v.getContext(), Detail.class);
  52.  
  53. Bundle b = new Bundle();
  54.  
  55. b.putString("id", "");
  56. b.putString("brandname", "");
  57. b.putString("alamat", "");
  58. b.putString("distance", "");
  59. b.putString("foto", "");
  60. v3.putExtras(b);
  61. v.getContext().startActivity(v3);
  62.  
  63. }
  64. });
  65. textView = (TextView) v.findViewById(R.id.judul);
  66. textView2 = (TextView) v.findViewById(R.id.deskripsi);
  67. textView3 = (TextView) v.findViewById(R.id.jarak);
  68. icon = (ImageView) v.findViewById(R.id.icon);
  69.  
  70.  
  71. }
  72.  
  73. public TextView getTextView() {
  74. return textView;
  75. }
  76.  
  77. public TextView getTextView2() {
  78. return textView2;
  79. }
  80. public TextView getTextView3() {
  81. return textView3;
  82. }
  83.  
  84. public ImageView getImageView() {
  85. return icon;
  86. }
  87. }
  88.  
  89. public NearbyAdapter(FragmentActivity activitys, List<Nearby> nearbys) {
  90.  
  91. this.activitys=activitys;
  92. this.brandname = new String[nearbys.size()];
  93. this.icon = new String[nearbys.size()];
  94. this.alamat = new String[nearbys.size()];
  95. this.distance = new String[nearbys.size()];
  96. this.status = new String[nearbys.size()];
  97. this.nearby = nearbys;
  98.  
  99.  
  100.  
  101. Log.i("Tag", "Name data " + new Gson().toJson(nearbys));
  102.  
  103. }
  104.  
  105. @Override
  106. public long getItemId(int position) {
  107.  
  108. return position;
  109. }
  110.  
  111. @Override
  112. public int getItemCount() {
  113. return nearby.size();
  114. }
  115.  
  116.  
  117. @Override
  118. public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
  119. // Create a new view.
  120. View v = LayoutInflater.from(viewGroup.getContext())
  121. .inflate(R.layout.list_nearby, viewGroup, false);
  122.  
  123. return new ViewHolder(v);
  124. }
  125.  
  126. @Override
  127. public void onBindViewHolder(ViewHolder viewHolder, final int position) {
  128. Log.d(TAG, "Element " + position + " set.");
  129. brandname[position] = nearby.get(position).getBrandName();
  130.  
  131. distance[position] = nearby.get(position).getDistance();
  132. icon[position] = nearby.get(position).getFoto();
  133. alamat[position] = nearby.get(position).getAddress();
  134. status[position] = nearby.get(position).getStatus();
  135.  
  136. Log.i("Tag", "Name data brandname" + new Gson().toJson(brandname[position]));
  137. Log.i("Tag", "Name data icon" + new Gson().toJson(icon[position]));
  138. Log.i("Tag", "Name data alamat" + new Gson().toJson(alamat[position]));
  139.  
  140. viewHolder.getTextView().setText(brandname[position]);
  141. viewHolder.getTextView2().setText(alamat[position]);
  142. viewHolder.getTextView3().setText(distance[position]);
  143.  
  144. Picasso.with(activitys)
  145. .load(icon[position])
  146. .error(R.drawable.boy)
  147. .into(viewHolder.getImageView());
  148.  
  149.  
  150. }
  151.  
  152.  
  153.  
  154.  
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment