Z
Z
zakkav2017-10-16 19:46:43
Android
zakkav, 2017-10-16 19:46:43

Why is data not showing in recyclerview with Json?

MainActivity

FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragment_container);
        if (fragment == null) {
            fragment =new ListFragment();
            fm.beginTransaction()
                    .add(R.id.fragment_container, fragment)
                    .commit();

fetchData
package com.example.zak.mycryptofragmentlist;

import android.os.AsyncTask;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class fetchData extends AsyncTask<Void,Void,Void> {
    String data ="";
    List<CurrencyModel> list=new ArrayList<>();

    @Override
    protected Void doInBackground(Void... voids) {
        try {
            URL url = new URL("https://api.coinmarketcap.com/v1/ticker/?limit=5");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            CurrencyModelLab.getInstance();
            while(line != null){
                line = bufferedReader.readLine();
                data = data + line;
            }
            JSONArray JA = new JSONArray(data);
            for(int i=0 ;i <JA.length(); i++){
                JSONObject JO = (JSONObject) JA.get(i);


                CurrencyModelLab.getInstance().addTocurrencyModelList(new CurrencyModel(JO.getString("name"),JO.getString("price_usd"),JO.getString("price_btc")));

            }



        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

          
    }
}


CurrencyModel

public class CurrencyModel {
    private String name;
    private String usdPrice;
    private String btcPrice;

    public static int count=0;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUsdPrice() {
        return usdPrice;
    }

    public void setUsdPrice(String usdPrice) {
        this.usdPrice = usdPrice;
    }

    public String getBtcPrice() {
        return btcPrice;
    }

    public void setBtcPrice(String btcPrice) {
        this.btcPrice = btcPrice;
    }

    public CurrencyModel(String name, String usdPrice, String btcPrice) {
        this.name = name;
        this.usdPrice = usdPrice;
        this.btcPrice = btcPrice;

    }
}


CurrencyModelLab

public class CurrencyModelLab {
    private static CurrencyModelLab instance = null;
    private List<CurrencyModel> currencyModelList=null;

    public void setCurrencyModelList(List<CurrencyModel> currencyModelList) {
        this.currencyModelList = currencyModelList;
    }

    private CurrencyModelLab() {
        if(currencyModelList==null)
           currencyModelList=new ArrayList<>();
      /*  for (int i = 0; i <20 ; i++) {
           currencyModelList.add(new CurrencyModel(i+"coin","100"+1,"200"+2));
        }*/
    }
    public static CurrencyModelLab getInstance() {
        if (instance == null) {
            instance = new CurrencyModelLab();
        }
        return instance;
    }
    public List<CurrencyModel> getCurrencyModelList(){
        return currencyModelList;
    }

    public void addTocurrencyModelList(CurrencyModel value){

        currencyModelList.add(value);
    }
}


ListFragment

public class ListFragment  extends Fragment {
        private RecyclerView mCrimeRecyclerView;
        private CrimeAdapter mAdapter;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_item_list, container, false);
            mCrimeRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
            mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
            fetchData process = new fetchData();
            process.execute();
            updateUI();
            return view;
        }
        private void updateUI() {
            CurrencyModelLab currencyModellab=CurrencyModelLab.getInstance();
            List<CurrencyModel> currencyModels = currencyModellab.getCurrencyModelList();
            mAdapter = new CrimeAdapter(currencyModels);
            mCrimeRecyclerView.setAdapter(mAdapter);
        }

        private class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            private TextView nameTextView;
            private TextView usdTextView;
            private TextView btcTextView;
            private CurrencyModel mCrime;
            public CrimeHolder(LayoutInflater inflater, ViewGroup parent) {
                super(inflater.inflate(R.layout.fragment_item, parent, false));
                itemView.setOnClickListener(this);
                nameTextView = (TextView) itemView.findViewById(R.id.nameView);
                btcTextView = (TextView) itemView.findViewById(R.id.priceBtcView);
                usdTextView = (TextView) itemView.findViewById(R.id.priceUsdView);

            }
            public void bind(CurrencyModel currencyModel) {
                mCrime = currencyModel;
                nameTextView.setText(mCrime.getName());
                usdTextView.setText(mCrime.getUsdPrice());
                btcTextView.setText(mCrime.getBtcPrice());
            }

            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(),
                        mCrime.getName() + " clicked!", Toast.LENGTH_SHORT)
                        .show();
            }
        }

        private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {
            private List<CurrencyModel> mCrimes;


            public CrimeAdapter(List<CurrencyModel> crimes) {
                mCrimes = crimes;
            }

            @Override
            public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
                return new CrimeHolder(layoutInflater, parent);
            }
            @Override
            public void onBindViewHolder(CrimeHolder holder, int position) {
                CurrencyModel currencyModel = mCrimes.get(position);
                holder.bind(currencyModel);
            }
            @Override
            public int getItemCount() {
                return mCrimes.size();
            }
        }
    }

59e4e2b2425a2231048467.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2017-10-16
@zakkav

Well, where, in fact, is the data update? onPostExecute does not have this. When adding to the list, nothing happens, but you need to notify the adapter about it.

M
midnight_blr, 2017-10-19
@midnight_blr

ListFragment.onCreateView(){
...
fetchData process = new fetchData();
process.execute();
updateUI();
...
}
You are trying to get the result of the background thread on the UI thread immediately after calling AsyncTask. Naturally, at the time of the updateUI(); your CurrencyModelLab model is not yet populated with data. I won’t even touch on the topic of concurrency, study it yourself.
How to fix it. All results of AsyncTask work should be obtained in its onPostExecute() method (by the way, it is already called on the UI thread). Those. You need to update the data on the UI in this method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question