我是一名新手android开发人员,我有不同货币项目的列表,我应该打开一个包含点击项目图表的新屏幕。
当用户单击列表项时,应打开一个新屏幕,该屏幕显示最近7天基于USD的所选货币的汇率图表。每当最近7天更新货币数据时,我都应该请求。
获取给定时间段内美元和加元之间的货币历史记录的示例请求:
https://api.exchangeratesapi.io/history?start_at=2019-11-27&end_at=2019-12-03&base=USD&symbols=CAD
这是我的代码:
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类
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;
}
}
我的模拟器屏幕
因此,如您所见,我想显示单击项的图表(给定期间内美元到单击项的7天之内的货币图表)
答案 0 :(得分:1)
我认为我应该像这样使用
XAxis
格式化值xAxis.setValueFormatter(here i should use a class);
详细说明我对另一个问题的答案,但不知道MPAndoirdChart,从文档中看来,您需要创建自己的ValueFormatter
子类并覆盖getFormattedValue(float)
以返回类似{ {1}}。您可能要使用LocalDate.ofEpochDay(Math.round(value)).toString()
来代替toString
。
这是一种快速尝试,未经测试:
DateTimeFormatter
我谦虚而暂定的建议是,您实例化此类的对象并将其传递给public class DateValueFormatter extends ValueFormatter {
@Override
String getFormattedValue(float value) {
int epochDay = Math.round(value);
LocalDate date = LocalDate.ofEpochDay(epochDay);
return date.toString();
}
}
。