Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.71 KB | None | 0 0
  1. package com.example.karim.bluecrunch;
  2.  
  3. /**
  4. * Created by karim on 8/26/16.
  5. */
  6. public class FlickrPhoto {
  7. String title , image ;
  8. public FlickrPhoto(String title , String image )
  9. {
  10. this.image = image;
  11. this.title = title;
  12. }
  13. }
  14.  
  15. package com.example.karim.bluecrunch;
  16. public class PhotosRecyclerViewAdapter extends RecyclerView.Adapter<PhotosRecyclerViewAdapter.PhotoViewHolder> {
  17.  
  18. List<FlickrPhoto> photos = Collections.emptyList();
  19. Context context;
  20. LayoutInflater inflater;
  21.  
  22. public PhotosRecyclerViewAdapter(Context context , List<FlickrPhoto> photos)
  23. {
  24. this.context = context;
  25. this.photos = photos;
  26. inflater = LayoutInflater.from(context);
  27. }
  28.  
  29. @Override
  30. public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  31. View row = inflater.inflate(R.layout.single_row,parent,false);
  32. return new PhotoViewHolder(row);
  33. }
  34.  
  35. @Override
  36. public void onBindViewHolder(PhotoViewHolder holder, int position) {
  37. FlickrPhoto photo = photos.get(position);
  38. holder.textView.setText(photo.title);
  39. Uri uri = Uri.parse(photo.image);
  40. Glide.with(context).load(uri).placeholder(R.drawable.image_placeholder).crossFade().into(holder.imageView);
  41.  
  42. }
  43.  
  44. @Override
  45. public int getItemCount() {
  46. return photos.size();
  47. }
  48.  
  49. public class PhotoViewHolder extends RecyclerView.ViewHolder {
  50. private TextView textView;
  51. private ImageView imageView;
  52.  
  53. public PhotoViewHolder(View itemView) {
  54. super(itemView);
  55. textView = (TextView) itemView.findViewById(R.id.photoTitle);
  56. imageView = (ImageView) itemView.findViewById(R.id.flickrPhoto);
  57. }
  58.  
  59.  
  60.  
  61. }
  62. }
  63.  
  64. package com.example.karim.bluecrunch;
  65.  
  66. public class PhotosListActivity extends AppCompatActivity {
  67. List<FlickrPhoto> photos ;
  68. ArrayList<String> photosTitles;
  69. ArrayList<String> photoURLS;
  70. String title;
  71. String uri;
  72. @Override
  73. protected void onCreate(Bundle savedInstanceState) {
  74. super.onCreate(savedInstanceState);
  75. setContentView(R.layout.activity_photos_list);
  76.  
  77. RecyclerView recyclerView = (RecyclerView) findViewById(R.id.photo_recycler_view);
  78. recyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(),2));
  79. PhotosRecyclerViewAdapter adapter = new PhotosRecyclerViewAdapter(this,getPhotos());
  80. recyclerView.setAdapter(adapter);
  81. }
  82.  
  83. public List<FlickrPhoto> getPhotos()
  84. {
  85.  
  86. photos = new ArrayList<>();
  87.  
  88. final String API_KEY = "fdac2e9676991ac53b34651adab52518";
  89. final String METHOD = "flickr.photos.search";
  90. final String AUTH_TOKEN = "72157671978046542-6e266595ffed01f8";
  91. final String API_SIG = "58e08d365779a8f2e946a2b5320199e2";
  92. final String FORMAT = "json";
  93. final int CALL_BACK = 1;
  94.  
  95. HandleRetrofit handleRetrofit = HandleRetrofit.retrofit.create(HandleRetrofit.class);
  96. Call<Photos> call = handleRetrofit.Photos(METHOD,API_KEY,FORMAT,CALL_BACK,AUTH_TOKEN,API_SIG);
  97. call.enqueue(new Callback<Photos>() {
  98. @Override
  99. public void onResponse(Call<Photos> call, Response<Photos> response) {
  100. Log.d("MainActivity", "Status Code = " + response.code());
  101. PhotosRetrofit photosRetrofit = response.body().photos;
  102. for (int i = 0; i < photosRetrofit.getPhoto().size(); i++) {
  103. uri="https://farm"+photosRetrofit.getPhoto().get(i).getFarm()+".staticflickr.com/"+
  104. photosRetrofit.getPhoto().get(i).getServer()+"/"+
  105. photosRetrofit.getPhoto().get(i).getId()+"_" +
  106. photosRetrofit.getPhoto().get(i).getSecret()+".jpg";
  107.  
  108. title= photosRetrofit.getPhoto().get(i).getTitle();
  109.  
  110. photos.add(new FlickrPhoto(title,uri));
  111.  
  112.  
  113. Log.w(">>>>>>>>>>>","https://farm"+photosRetrofit.getPhoto().get(i).getFarm()+".staticflickr.com/"+
  114. photosRetrofit.getPhoto().get(i).getServer()+"/"+
  115. photosRetrofit.getPhoto().get(i).getId()+"_" +
  116. photosRetrofit.getPhoto().get(i).getSecret()+".jpg");
  117. }
  118.  
  119. }
  120.  
  121. @Override
  122. public void onFailure(Call<Photos> call, Throwable t) {
  123. Toast.makeText(PhotosListActivity.this,"Error :"+t.getMessage(),Toast.LENGTH_LONG).show();
  124. Log.w("---___--- Error ",t.getMessage());
  125. }
  126. });
  127. /* Log.w("Hello",uri.toString());
  128. Log.w("Hello",title.toString());*/
  129. photos.add(new FlickrPhoto("karim","https://farm9.staticflickr.com/8450/29141814932_a62977990d.jpg"));
  130. return photos;
  131. }
  132. }
  133.  
  134. photos.add(new FlickrPhoto(title,uri));
  135.  
  136. photos.add(new FlickrPhoto("karim","https://farm9.staticflickr.com/8450/29141814932_a62977990d.jpg"));
  137.  
  138. for (int i = 0; i < photosRetrofit.getPhoto().size(); i++)
  139. {
  140. uri="https://farm"... ;
  141. title= ..;
  142. **photos.add(new FlickrPhoto(title,uri));**
  143. Log.w(">>>>>>>>>>>","https://farm"+photosRetrofit.getPhoto()‌​.get(i).getFarm()+".‌​staticflickr.com/"+p‌​hotosRetrofit.getPho‌​to().get(i).getServe‌​r()+"/"+photosRetrof‌​it.getPhoto().get(i)‌​.getId()+"_" +photosRetrofit.getPhoto().get(i).getSecret()+".jpg");
  144. }
  145.  
  146. package com.example.karim.bluecrunch;
  147.  
  148. /**
  149. * Created by karim on 8/26/16.
  150. */
  151. public class FlickrPhoto {
  152. String title , image ;
  153. public FlickrPhoto(){}
  154. public FlickrPhoto(String title , String image )
  155. {
  156. this.image = image;
  157. this.title = title;
  158. }
  159.  
  160. public String getTitle() {
  161. return title;
  162. }
  163.  
  164. public void setTitle(String title) {
  165. this.title = title;
  166. }
  167.  
  168. public String getImage() {
  169. return image;
  170. }
  171.  
  172. public void setImage(String image) {
  173. this.image = image;
  174. }
  175.  
  176. }
  177.  
  178. package com.example.karim.bluecrunch;
  179.  
  180. import android.net.Uri;
  181. import android.support.v7.app.AppCompatActivity;
  182. import android.os.Bundle;
  183. import android.support.v7.widget.GridLayoutManager;
  184. import android.support.v7.widget.LinearLayoutManager;
  185. import android.support.v7.widget.RecyclerView;
  186. import android.util.Log;
  187. import android.widget.TextView;
  188. import android.widget.Toast;
  189.  
  190. import java.net.URI;
  191. import java.util.ArrayList;
  192. import java.util.List;
  193.  
  194. import retrofit2.Call;
  195. import retrofit2.Callback;
  196. import retrofit2.Response;
  197. import retrofit2.http.Url;
  198.  
  199. public class PhotosListActivity extends AppCompatActivity {
  200. List<FlickrPhoto> photos ;
  201. ArrayList<String> photosTitles;
  202. ArrayList<String> photoURLS;
  203. FlickrPhoto flickrPhoto ;
  204. String title;
  205. String uri;
  206. @Override
  207. protected void onCreate(Bundle savedInstanceState) {
  208. super.onCreate(savedInstanceState);
  209. setContentView(R.layout.activity_photos_list);
  210.  
  211. RecyclerView recyclerView = (RecyclerView) findViewById(R.id.photo_recycler_view);
  212. recyclerView.setLayoutManager(new GridLayoutManager(this,2));
  213. PhotosRecyclerViewAdapter adapter = new PhotosRecyclerViewAdapter(this,getPhotos());
  214. recyclerView.setAdapter(adapter);
  215.  
  216. flickrPhoto = new FlickrPhoto();
  217.  
  218.  
  219.  
  220. }
  221.  
  222. public List<FlickrPhoto> getPhotos()
  223. {
  224.  
  225. photos = new ArrayList<>();
  226.  
  227. final String API_KEY = "fdac2e9676991ac53b34651adab52518";
  228. final String METHOD = "flickr.photos.search";
  229. final String AUTH_TOKEN = "72157671978046542-6e266595ffed01f8";
  230. final String API_SIG = "58e08d365779a8f2e946a2b5320199e2";
  231. final String FORMAT = "json";
  232. final int CALL_BACK = 1;
  233.  
  234. HandleRetrofit handleRetrofit = HandleRetrofit.retrofit.create(HandleRetrofit.class);
  235. Call<Photos> call = handleRetrofit.Photos(METHOD,API_KEY,FORMAT,CALL_BACK,AUTH_TOKEN,API_SIG);
  236. call.enqueue(new Callback<Photos>() {
  237. @Override
  238. public void onResponse(Call<Photos> call, Response<Photos> response) {
  239. Log.d("MainActivity", "Status Code = " + response.code());
  240. PhotosRetrofit photosRetrofit = response.body().photos;
  241. //photosRetrofit.getPhoto().size()
  242. for (int i = 0; i < photosRetrofit.getPhoto().size(); i++) {
  243. uri="https://farm"+photosRetrofit.getPhoto().get(i).getFarm()+".staticflickr.com/"+
  244. photosRetrofit.getPhoto().get(i).getServer()+"/"+
  245. photosRetrofit.getPhoto().get(i).getId()+"_" +
  246. photosRetrofit.getPhoto().get(i).getSecret()+".jpg";
  247.  
  248. title= photosRetrofit.getPhoto().get(i).getTitle();
  249.  
  250. flickrPhoto.setImage(uri);
  251. flickrPhoto.setTitle(title);
  252.  
  253. photos.add(new FlickrPhoto(flickrPhoto.getTitle(),flickrPhoto.getImage()));
  254.  
  255.  
  256. Log.w("++++++++++++",photosRetrofit.getPhoto().get(i).getTitle());
  257.  
  258. Log.w(">>>>>>>>>>>","https://farm"+photosRetrofit.getPhoto().get(i).getFarm()+".staticflickr.com/"+
  259. photosRetrofit.getPhoto().get(i).getServer()+"/"+
  260. photosRetrofit.getPhoto().get(i).getId()+"_" +
  261. photosRetrofit.getPhoto().get(i).getSecret()+".jpg");
  262.  
  263.  
  264.  
  265. }
  266.  
  267. }
  268.  
  269. @Override
  270. public void onFailure(Call<Photos> call, Throwable t) {
  271. Toast.makeText(PhotosListActivity.this,"Error :"+t.getMessage(),Toast.LENGTH_LONG).show();
  272. Log.w("---___--- Error ",t.getMessage());
  273. }
  274. });
  275. /* Log.w("Hello",uri.toString());
  276. Log.w("Hello",title.toString());*/
  277. photos.add(new FlickrPhoto("karim","https://farm9.staticflickr.com/8450/29141814932_a62977990d.jpg"));
  278. return photos;
  279. }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement