I am newbie android developer and i have a list of different currency items and I should open a new screen with chart of clicked item.
when user clicks on a list item a new screen should be opened which shows the exchange rate chart of the selected currency for the last 7 days based on USD. I should request every time the currency data gets updated for the last 7 days.
Example request for getting currency history in a given period between USD and CAD:
https://api.exchangeratesapi.io/history?start_at=2019-11-27&end_at=2019-12-03&base=USD&symbols=CAD
Here is My code:
MainActivity
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progress_bar);
new GetServerData(this).execute();
}
private static class GetServerData extends AsyncTask<Integer, Void, List<CurrencyRate>> {
private static final int TIMEOUT = 30000;
private static final String BASE_URL = "https://api.exchangeratesapi.io/latest?base=USD";
private WeakReference<MainActivity> activityReference;
GetServerData(MainActivity context) {
activityReference = new WeakReference<>(context);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
MainActivity activity = activityReference.get();
if (activity == null || activity.isFinishing()) {
return;
}
activity.progressBar.setVisibility(View.VISIBLE);
}
@Override
protected List<CurrencyRate> doInBackground(Integer... integers) {
List<CurrencyRate> currencyList = new ArrayList<>();
OkHttpClient client = new OkHttpClient().newBuilder()
.readTimeout(TIMEOUT, TimeUnit.SECONDS)
.connectTimeout(TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(TIMEOUT, TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.build();
Request request = new Request.Builder()
.url(BASE_URL)
.build();
try {
Response response = client.newCall(request).execute();
Log.d("Response", response.toString());
long tx = response.sentRequestAtMillis();
long rx = response.receivedResponseAtMillis();
System.out.println("response time : " + (rx - tx) + " ms");
JSONObject object = new JSONObject(Objects.requireNonNull(response.body()).string());
JSONObject rates = object.getJSONObject("rates");
Iterator<String> iterator = rates.keys();
while (iterator.hasNext()) {
String key = iterator.next();
String value = rates.getString(key);
CurrencyRate data = new CurrencyRate(key, value);
currencyList.add(data);
}
} catch (IOException | JSONException e) {
e.printStackTrace();
Log.d("MainActivity", e.toString());
}
return currencyList;
}
@Override
protected void onPostExecute(final List<CurrencyRate> result) {
final MainActivity activity = activityReference.get();
if (activity == null || activity.isFinishing()) {
return;
}
ListView listView = activity.findViewById(R.id.list_view);
CurrencyAdapter adapter = new CurrencyAdapter(activity, result);
listView.setAdapter(adapter);
listView.smoothScrollToPosition(0);
adapter.notifyDataSetChanged();
activity.progressBar.setVisibility(View.GONE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(activity.getApplicationContext(), result.get(i).getName()+" Clicked", Toast.LENGTH_SHORT).show();
/*Which code should be here to open a new screen with exchange chart for last 7 days of clicked item??*/
}
});
}
}
}
CurrencyRate class
public class CurrencyRate {
private String name;
private String value;
public CurrencyRate(String name, String value) {
super();
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
My emulator screen
so as you see the items,i want to show chart of clicked item (chart of currency in a given period 7 days between USD and Clicked item)