Advertisement
corovino

andrpod postAdapter

Feb 9th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.71 KB | None | 0 0
  1. package apps.catalogo.kennyromero.catalogoapps.Adapters;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.util.Log;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.ArrayAdapter;
  10. import android.widget.ImageView;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13.  
  14. import com.android.volley.Request;
  15. import com.android.volley.RequestQueue;
  16. import com.android.volley.Response;
  17. import com.android.volley.VolleyError;
  18. import com.android.volley.toolbox.ImageLoader;
  19. import com.android.volley.toolbox.ImageRequest;
  20. import com.android.volley.toolbox.JsonObjectRequest;
  21. import com.android.volley.toolbox.NetworkImageView;
  22. import com.android.volley.toolbox.Volley;
  23.  
  24.  
  25. import org.json.JSONArray;
  26. import org.json.JSONException;
  27. import org.json.JSONObject;
  28.  
  29. import java.util.ArrayList;
  30. import java.util.List;
  31.  
  32. import apps.catalogo.kennyromero.catalogoapps.R;
  33. import apps.catalogo.kennyromero.catalogoapps.data.ListaPost;
  34. import apps.catalogo.kennyromero.catalogoapps.data.Post;
  35.  
  36. /**
  37. * Created by kenny.romero on 06/02/2016.
  38. */
  39. public class aPostAdapter extends ArrayAdapter {
  40.  
  41. // Atributos
  42. private String URL_BASE = "https://itunes.apple.com/us/rss/topfreeapplications/limit=20/json";
  43. private static final String TAG = "PostAdapter";
  44. private JsonObjectRequest jsArrayRequest ;
  45. private RequestQueue requestQueue;
  46. List<Post> items;
  47.  
  48. public PostAdapter(Context context) {
  49. super(context,0);
  50.  
  51. // Crear nueva cola de peticiones
  52. requestQueue= Volley.newRequestQueue(context);
  53.  
  54. // Nueva petición JSONObject
  55. jsArrayRequest = new JsonObjectRequest(
  56. Request.Method.GET,
  57. URL_BASE ,
  58. (String)null,
  59. new Response.Listener<JSONObject>() {
  60. @Override
  61. public void onResponse(JSONObject response) {
  62. items = parseJson(response);
  63. notifyDataSetChanged();
  64. }
  65. },
  66. new Response.ErrorListener() {
  67. @Override
  68. public void onErrorResponse(VolleyError error) {
  69. Log.d(TAG, "Error Respuesta en JSON: " + error.getMessage());
  70.  
  71. }
  72. }
  73. );
  74.  
  75. // Añadir petición a la cola
  76. requestQueue.add(jsArrayRequest);
  77. }
  78.  
  79. @Override
  80. public int getCount() {
  81. return items != null ? items.size() : 0;
  82. }
  83.  
  84.  
  85.  
  86. @Override
  87. public View getView(int position, View convertView, ViewGroup parent) {
  88.  
  89. LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
  90.  
  91. // Referencia del view procesado
  92. View listItemView;
  93.  
  94. //Comprobando si el View no existe
  95. listItemView = null == convertView ? layoutInflater.inflate(
  96. R.layout.grid_item,
  97. parent,
  98. false) : convertView;
  99.  
  100.  
  101. // Obtener el item actual
  102. Post item = items.get(position);
  103.  
  104. // Obtener Views
  105. TextView textoTitulo = (TextView) listItemView.
  106. findViewById(R.id.nameItem_app);
  107. //TextView textoDescripcion = (TextView) listItemView.
  108. // findViewById(R.id.textoDescripcion);
  109. final ImageView imagenPost = (ImageView) listItemView.
  110. findViewById(R.id.imgItem_app);
  111.  
  112. // Actualizar los Views
  113. textoTitulo.setText(item.getIm_name());
  114. //textoDescripcion.setText(item.getDescripcion());
  115.  
  116. // Petición para obtener la imagen
  117. ImageRequest request = new ImageRequest(
  118. item.getIm_image(), //url img icon
  119. new Response.Listener<Bitmap>() {
  120. @Override
  121. public void onResponse(Bitmap bitmap) {
  122. imagenPost.setImageBitmap(bitmap);
  123. }
  124. }, 0, 0, null,null,
  125. new Response.ErrorListener() {
  126. public void onErrorResponse(VolleyError error) {
  127. imagenPost.setImageResource(R.drawable.box);
  128. Log.d(TAG, "Error en respuesta Bitmap: "+ error.getMessage());
  129. }
  130. });
  131.  
  132. // Añadir petición a la cola
  133. requestQueue.add(request);
  134.  
  135.  
  136. return listItemView;
  137. }
  138.  
  139.  
  140. public List<Post> parseJson(JSONObject jsonObject){
  141. // Variables locales
  142. List<Post> posts = new ArrayList();
  143. JSONObject feed = null;
  144. JSONArray entry = null;
  145.  
  146. try {
  147. // Obtener el priemr objeto
  148. feed = jsonObject.getJSONObject("feed");
  149. entry = feed.getJSONArray("entry");
  150.  
  151. for(int i=0; i<entry.length(); i++){
  152.  
  153. try {
  154. JSONObject objeto= entry.getJSONObject(i);
  155. JSONObject name= objeto.getJSONObject("im:name");
  156. JSONArray imagen = objeto.getJSONArray("im:image");
  157. JSONObject img1 = imagen.getJSONObject(0);
  158. JSONObject img2 = imagen.getJSONObject(2);
  159. JSONObject summary = objeto.getJSONObject("summary");
  160. JSONObject imPrice = objeto.getJSONObject("im:price");
  161. JSONObject price = imPrice.getJSONObject("attributes");
  162. JSONObject contentType = objeto.getJSONObject("im:contentType");
  163. JSONObject attributesCt = contentType.getJSONObject("attributes");
  164. JSONObject rights = objeto.getJSONObject("rights");
  165. JSONObject title = objeto.getJSONObject("title");
  166. JSONObject link = objeto.getJSONObject("link");
  167. JSONObject attributesLk = link.getJSONObject("attributes");
  168. JSONObject id = objeto.getJSONObject("id");
  169. //JSONObject attributesId = id.getJSONObject("im:id");
  170. JSONObject artist = objeto.getJSONObject("im:artist");
  171. JSONObject linkArtist = artist.getJSONObject("attributes");
  172. JSONObject category = objeto.getJSONObject("category");
  173. JSONObject attributesCty = category.getJSONObject("attributes");
  174. JSONObject releaseDate = objeto.getJSONObject("im:releaseDate");
  175. JSONObject attributesRelease = releaseDate.getJSONObject("attributes");
  176.  
  177.  
  178. Post post = new Post(
  179. name.getString("label"),
  180. img1.getString("label"),
  181. img2.getString("label"),
  182. summary.getString("label"),
  183. price.getString("amount"),
  184. price.getString("currency"),
  185. attributesCt.getString("label"),
  186. rights.getString("label"),
  187. title.getString("label"),
  188. attributesLk.getString("href"),
  189. id.getString("label"),
  190. artist.getString("label"),
  191. linkArtist.getString("href"),
  192. attributesCty.getString("label"),
  193. attributesRelease.getString("label"));
  194.  
  195.  
  196. posts.add(post);
  197.  
  198. } catch (JSONException e) {
  199. Log.e(TAG, "Error de parsing: "+ e.getMessage());
  200. }
  201. }
  202.  
  203. } catch (JSONException e) {
  204. e.printStackTrace();
  205. }
  206.  
  207.  
  208. return posts;
  209. }
  210.  
  211.  
  212.  
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement