我有一项服务,可以从我的webservice
中删除一个对象并返回当前列表。每次我请求服务时,它都会更新我的textview
(具有列表的大小)和适配器。问题是当我转到MainActivity
并返回执行此服务的活动时,textview
和适配器不再更新。
apiServices.deleteEmployee(new APIServices.BookmarkCallback() {
@Override
public void onSuccess(final ArrayList<Employee> list) {
remainingPeople.setText(getResources().getString(R.string.remaining_people) + list.size());
adapter = new ArrayAdapter<>(getApplicationContext(),
android.R.layout.simple_list_item_1, list);
employeesList.setAdapter(adapter);
}
在我的onCreate()
方法上设置了代码。
返回活动后仍会更新textview
。
onCreate()
方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//set content view AFTER ABOVE sequence (to avoid crash)
this.setContentView(R.layout.activity_count);
remainingPeople = findViewById(R.id.peopleCounter);
findBT();
openBT();
@SuppressWarnings("unchecked")
ArrayList<Employee> list = (ArrayList<Employee>) getIntent()
.getSerializableExtra(SEND_EMPLOYEES_LIST);
remainingPeople.setText(getResources().getString(R.string.remaining_people) + list.size());
employeesList = findViewById(R.id.employees_list);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
employeesList.setAdapter(adapter);
beep = new ToneGenerator(AudioManager.STREAM_ALARM, 900);
handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message message) {
switch (message.what) {
case READ_ARRAY_BYTE:
byte[] arrayBytes = new byte[message.arg1];
System.arraycopy(message.obj, 0, arrayBytes, 0, message.arg1);
try {
String epc = new String(arrayBytes, "UTF-8");
beep.stopTone();
epc = epc.substring(0, 24);
apiServices.deleteEmployee(new APIServices.BookmarkCallback() {
@Override
public void onSuccess(final ArrayList<Employee> list) {
//this part updates after the API response
remainingPeople.setText(getResources().getString(R.string.remaining_people) + list.size());
adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, list);
employeesList.setAdapter(adapter);
}
@Override
public void onError() {
}
}, epc);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
break;
//...
答案 0 :(得分:0)
尝试将更新textview
而不是onResume()
的过程放在onCreate()
@Override
public void onResume(){
super.onResume();
// put your code here...
}
答案 1 :(得分:0)
我通过michiakig's answer找到了一个解决方案,可以将文本视图声明为static
,但是我可能会遇到问题。因此,另一种方法是使用Activity.onSaveInstanceState
保存。
谢谢所有回答我的人。