【发布时间】:2019-12-12 14:24:14
【问题描述】:
我是新手 android 开发人员,我有一个不同货币项目的列表,我应该打开一个带有点击项目图表的新屏幕。
当用户单击列表项时,应打开一个新屏幕,显示过去 7 天所选货币基于美元的汇率图表。我应该在过去 7 天内每次更新货币数据时请求。
获取给定时间段内美元和加元之间的货币历史记录的示例请求:
https://api.exchangeratesapi.io/history?start_at=2019-11-27&end_at=2019-12-03&base=USD&symbols=CAD
这是我的代码:
主活动
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 天的货币图表)
【问题讨论】:
-
你的问题不是很宽泛吗?你的搜索在哪里?你的尝试,你的研究?不懂Android编程的问(只擅长Java)。
-
@OleV.V.我在 Github 有自己的新闻应用程序,你可以看看我是否只擅长 JAVA :) Github Link.
-
@OleV.V.我已经解决了这个问题,但是你告诉我的关于
LocaleDate我仍然无法将它转换为long值我做了一个长数组。LocalDate date1 = LocalDate.parse(keyDate); int []date = new int[]{date1.getYear(),date1.getMonthValue(),date1.getDayOfMonth()}; StringBuilder builder = new StringBuilder(); for (int i :date) { builder.append(i); } long v = Long.parseLong(builder.toString());结果是20191127!!!我如何在没有String的情况下将此数字转换为 2019-11-27。 -
@OleV.V.我认为我应该使用
XAxis来格式化值,就像xAxis.setValueFormatter(here i should use a class);我应该找到类似于这个问题Stackoverflow。
标签: java android android-intent retrofit mpandroidchart