- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 volley 包从网站 (JSON) 检索数据。
这是我的方法
private void getEarthquakeList(){
// ...
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
//Earthquake Feeder
String url ="https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_month.geojson";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Response is: ",response);
//Parsing Json
parseJson(response);
final ListView earthquakeListView = (ListView)findViewById(R.id.list);
//Sort the array according to magnitude
earthquakeArrayList.sort((a, b) -> Double.compare(b.getTime(), a.getTime()));
mAdapter = new EarthquakeAdapter(getApplicationContext(), earthquakeArrayList);
earthquakeListView.setAdapter(mAdapter);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error",error.getMessage());
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
问题是,现在我正在该方法返回响应后更新 UI。
这些是行
//Sort the array according to a date
earthquakeArrayList.sort((a, b) -> Double.compare(b.getTime(), a.getTime()));
mAdapter = new EarthquakeAdapter(getApplicationContext(), earthquakeArrayList);
earthquakeListView.setAdapter(mAdapter);
当我在 MainActivity 中运行时没有问题,用户打开应用程序并获取地震列表。
当我想切换到我每隔几分钟监控一次的服务时,或者当网站内容发生变化时,问题就开始了。所以我想要我的方法而不更新其中的 UI。问题是,如果我不更新 onResponse 内的 UI,UI 线程将继续并导致一个空数组。
所以如果我不在里面做这个数组earthquakeArrayList
保持空
public void onResponse(String response)
想法如何将两者分开,一方面运行方法并获取数据,另一方面主线程将能够访问数据而不是完成执行。谢谢EG
最佳答案
目前推荐的解决方案是使用 LiveData . LiveData 适用于 Observer Pattern ,因此您可以让任意数量的观察者观察服务和 Activity 中的更新数据,但他们需要处于相同的上下文中。这些主题有点高级,但却是将 UI 与数据层分离的好方法。你可以通过这两个链接
关于Android等待截击完成并稍后更新用户界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53911606/
我正在编写一个插件,有一个ajax调用向用户显示数据。 如果用户想在ajax成功时添加一些js?他可以从他的 js 脚本中做到这一点吗,比如定位这个 ajax 成功事件。 例如: $(documen
我有 html 代码,例如 - x 最初插入 div 'insert_calendar_eda_form'。 Javascript代码 calendar_eda_add
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 3 年前。 Improve this qu
我已经使用命令 sudo start myservice 启动了一个 upstart 服务。我想要一种方法,以便稍后我(或任何人)可以检查该服务是否已启动并正在运行。检查它的命令是什么? 最佳答案 找
我是一名优秀的程序员,十分优秀!