是否可以通过Web服务器api使用MpAndroid来显示图表? [英] Is it possible to show a chart by using MpAndroid from web server api?
问题描述
我是一名新手android开发人员,我有一个不同货币项目的列表,我应该打开一个包含点击项目图表的新屏幕.
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.
当用户单击列表项时,应打开一个新屏幕,该屏幕显示最近7天基于USD的所选货币的汇率图表.我应该在最近7天每次更新货币数据时都要求.
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
这是我的代码:
MainActivity
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类
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
因此,如您所见,我想显示单击项的图表(给定时间段内美元到单击项之间的7天之内的货币图表)
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)
推荐答案
我认为我应该像这样使用
XAxis
格式化值xAxis.setValueFormatter(here i should use a class);
I think that i should format the value by using
XAxis
like thatxAxis.setValueFormatter(here i should use a class);
详细说明我对另一个问题的答案,但不了解MPAndoirdChart,从文档中看来,您需要创建自己的ValueFormatter
子类并覆盖getFormattedValue(float)
以返回类似LocalDate.ofEpochDay(Math.round(value)).toString()
的内容.您可能要使用DateTimeFormatter
代替toString
.
Elaborating on my answer to your other question and not knowing MPAndoirdChart, it would seem to me from the documentation that you need to make your own subclass of ValueFormatter
and override getFormattedValue(float)
to return something like LocalDate.ofEpochDay(Math.round(value)).toString()
. Instead of toString
you may want to use a DateTimeFormatter
.
这是一种快速尝试,未经测试:
Here’s a quick attempt, not tested:
public class DateValueFormatter extends ValueFormatter {
@Override
String getFormattedValue(float value) {
int epochDay = Math.round(value);
LocalDate date = LocalDate.ofEpochDay(epochDay);
return date.toString();
}
}
我谦虚而暂定的建议是,您实例化此类的对象并将其传递给xAxis.setValueFormatter()
.
My humble and tentative suggestion is that you instantiate an object of this class and pass it to xAxis.setValueFormatter()
.
- My answer to your question java.lang.NumberFormatException: For input string: "2019-11-27"
- Documentation of
com.github.mikephil.charting.formatter.ValueFormatter
- Documentation of
DateTimeFormatter
这篇关于是否可以通过Web服务器api使用MpAndroid来显示图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!