Receive information from the network and attach that data to the view.
Use Ion library,
private void getListInfo() {
Ion.with(getApplicationContext())
.load("POST", "URL..")
.setBodyParameter("Parameters", "Parameters")
.asString()
.withResponse()
.setCallback(new FutureCallback<Response<String>>() {
@Override
public void onCompleted(Exception e, Response<String> result) {
if (e != null) {
e.printStackTrace();
} else {
int resultCode = result.getHeaders().code();
String resultInfo = result.getResult();
if (resultCode == 200) {
Log.d("EntrantInfo", resultInfo);
Gson gson = new Gson();
Type type = new TypeToken<List<Data>>() {
}.getType();
listItems = gson.fromJson(resultInfo, type);
Adapter.setItems(listItems);
Adapter.notifyDataSetChanged();
textview.setText(data);
}
}
}
});
}
So, I call the getListInfo()
method at onCreate()
.
I move that 'A' activity, I can't see the data at all.
In recyclerview or listview, use notifyDataSetChanged()
but I can't see the list.
But, when I move back and move 'A' activity again, I can see the data and view.
So I think I have got information, but it did not update in that view.
Now, how I can see the view when I get the information from server?
You can use a SwipeRefreshLayout to refresh your data on swipe : SwipeRefreshLayout
Add the data loading function to the SwipeRefreshLayout.OnRefreshListener
Or
You can set a TimerTask to refresh your data
TimerTask timerTask;
Timer timer;
timerTask = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
//load your data
getListInfo();
}
});
}
};
timer = new Timer();
timer.scheduleAtFixedRate(timerTask, 0, 200);
And make sure to cancel the timer onPause()
@Override
protected void onPause() {
if(timer != null){
timer.cancel();
}
super.onPause();
}