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();
}
}
}