Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void updateAdapterView(JSONArray cards) {
- //Error gets triggered here
- grid.invalidateViews();
- adapter.refresh(cards);
- }
- //Set Layout for Fragment
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- final FrameLayout view = (FrameLayout) inflater.inflate(R.layout.fragment_exchange, container, false);
- loadinglayout = view.findViewById(R.id.loadinglayout);
- gridlayout = view.findViewById(R.id.gridlayout);
- //first time setting Adapter with empty items
- adapter = new ExchangeListAdapter(getContext(), cards);
- grid = view.findViewById(R.id.grid);
- grid.setAdapter(adapter);
- grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- Intent exchangedetail = new Intent(getActivity(), ExchangeDetail.class);
- //exchangedetail.putExtra("item", cardslist[+ position]);
- getActivity().startActivity(exchangedetail);
- }
- });
- return view;
- }
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- //get data from firestore
- ((MainActivity) getActivity()).getCardsList(new BaseAppCompatActivity.OnCardsFilledListener() {
- @Override
- public void onCardsFilled(final JSONArray cards) {
- updateAdapterView(cards);
- }
- @Override
- public void onError(Exception taskException) {
- }
- });
- }
- public void updateAdapterView(JSONArray cards) {
- grid.invalidateViews();
- adapter.refresh(cards);
- }
- public class ExchangeListAdapter extends BaseAdapter {
- private Context mContext;
- private JSONArray cards;
- public ExchangeListAdapter(Context c, JSONArray cards) {
- mContext = c;
- this.cards = cards;
- }
- @Override
- public int getCount() {
- int length = 0;
- if(cards != null) {
- length = cards.length();
- }
- return length;
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return 0;
- }
- public void refresh(JSONArray cards)
- {
- this.cards = cards;
- notifyDataSetChanged();
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- View grid;
- LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- if (convertView == null) {
- grid = new View(mContext);
- grid = inflater.inflate(R.layout.item_exchange_list, null);
- ImageView imageView = grid.findViewById(R.id.grid_image);
- URL url = null;
- try {
- url = new URL(cards.getJSONObject(position).getString("thumbnail"));
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (JSONException e) {
- e.printStackTrace();
- }
- Bitmap bmp = null;
- try {
- bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
- } catch (IOException e) {
- e.printStackTrace();
- }
- imageView.setImageBitmap(bmp);
- } else {
- grid = convertView;
- }
- return grid;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement