Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. public void updateAdapterView(JSONArray cards) {
  2. //Error gets triggered here
  3. grid.invalidateViews();
  4. adapter.refresh(cards);
  5. }
  6.  
  7. //Set Layout for Fragment
  8. @Override
  9. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  10. final FrameLayout view = (FrameLayout) inflater.inflate(R.layout.fragment_exchange, container, false);
  11. loadinglayout = view.findViewById(R.id.loadinglayout);
  12. gridlayout = view.findViewById(R.id.gridlayout);
  13.  
  14. //first time setting Adapter with empty items
  15. adapter = new ExchangeListAdapter(getContext(), cards);
  16. grid = view.findViewById(R.id.grid);
  17. grid.setAdapter(adapter);
  18. grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  19. @Override
  20. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  21. Intent exchangedetail = new Intent(getActivity(), ExchangeDetail.class);
  22. //exchangedetail.putExtra("item", cardslist[+ position]);
  23. getActivity().startActivity(exchangedetail);
  24. }
  25. });
  26.  
  27. return view;
  28. }
  29.  
  30. @Override
  31. public void onActivityCreated(Bundle savedInstanceState) {
  32. super.onActivityCreated(savedInstanceState);
  33.  
  34. //get data from firestore
  35. ((MainActivity) getActivity()).getCardsList(new BaseAppCompatActivity.OnCardsFilledListener() {
  36. @Override
  37. public void onCardsFilled(final JSONArray cards) {
  38. updateAdapterView(cards);
  39. }
  40.  
  41. @Override
  42. public void onError(Exception taskException) {
  43.  
  44. }
  45. });
  46. }
  47.  
  48. public void updateAdapterView(JSONArray cards) {
  49. grid.invalidateViews();
  50. adapter.refresh(cards);
  51. }
  52.  
  53. public class ExchangeListAdapter extends BaseAdapter {
  54. private Context mContext;
  55. private JSONArray cards;
  56.  
  57. public ExchangeListAdapter(Context c, JSONArray cards) {
  58. mContext = c;
  59. this.cards = cards;
  60. }
  61.  
  62. @Override
  63. public int getCount() {
  64. int length = 0;
  65.  
  66. if(cards != null) {
  67. length = cards.length();
  68. }
  69.  
  70. return length;
  71. }
  72.  
  73. @Override
  74. public Object getItem(int position) {
  75. // TODO Auto-generated method stub
  76. return null;
  77. }
  78.  
  79. @Override
  80. public long getItemId(int position) {
  81. // TODO Auto-generated method stub
  82. return 0;
  83. }
  84.  
  85. public void refresh(JSONArray cards)
  86. {
  87. this.cards = cards;
  88. notifyDataSetChanged();
  89. }
  90.  
  91. @Override
  92. public View getView(int position, View convertView, ViewGroup parent) {
  93. View grid;
  94. LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  95.  
  96. if (convertView == null) {
  97. grid = new View(mContext);
  98. grid = inflater.inflate(R.layout.item_exchange_list, null);
  99. ImageView imageView = grid.findViewById(R.id.grid_image);
  100.  
  101. URL url = null;
  102. try {
  103. url = new URL(cards.getJSONObject(position).getString("thumbnail"));
  104. } catch (MalformedURLException e) {
  105. e.printStackTrace();
  106. } catch (JSONException e) {
  107. e.printStackTrace();
  108. }
  109.  
  110. Bitmap bmp = null;
  111. try {
  112. bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
  113. } catch (IOException e) {
  114. e.printStackTrace();
  115. }
  116.  
  117. imageView.setImageBitmap(bmp);
  118. } else {
  119. grid = convertView;
  120. }
  121.  
  122. return grid;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement