我知道这个话题已经存在一些问题 我访问了每个链接,但没有帮助。
我要做的是从MySQL获取值并平行更新UI而不使用AsyncTask。
我正在根据我从另一个Activity获取的唯一ID获取值。
我认为因为我使用的是Volley库,所以我不需要AsyncTask 我在吐司中从数据库中获取值,但我无法更新UI。
这是我的班级
public class DisplayInformationActivity extends Activity implements AppCompatCallback{
private CollapsingToolbarLayout collapsingToolbarLayout;
protected AppCompatDelegate delegate;
protected String UniqueID;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String,List<String>> listDataChild;
private ProgressDialog loading;
private String Name = "";
private String contact = "";
private String email = "";
protected void onCreate(Bundle savedInstanceState){
UniqueID = getIntent().getStringExtra("uniqueID");
Toast.makeText(this,UniqueID, Toast.LENGTH_LONG).show();
fetchEverything(UniqueID);
super.onCreate(savedInstanceState);
delegate = AppCompatDelegate.create(this,this);
delegate.onCreate(savedInstanceState);
delegate.setContentView(R.layout.displayhallinformation);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
delegate.setSupportActionBar(toolbar);
android.app.ActionBar actionBar = getActionBar();
if(actionBar!=null)
actionBar.setDisplayHomeAsUpEnabled(true);
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbarLayout.setTitle(Name);
dynamicToolbarColor();
toolBarTextAppearance();
expListView = (ExpandableListView) findViewById(R.id.expandableListView);
prepareListData();
listAdapter = new ExpandableListAdapter(this,listDataHeader,listDataChild);
expListView.setAdapter(listAdapter);
}
private void fetchEverything(String uniqueID) {
RequestQueue mRequestQueue = AppController.getInstance().getRequestQueue();
mRequestQueue.start();
loading = ProgressDialog.show(this,"Loading Information ...","",false,false);
loading.setCancelable(false);
String url = AppConfig.URL_DisplayInformation+"?"+"uniqueID="+uniqueID;
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
AppController.getInstance().addToRequestQueue(stringRequest);
}
private void showJSON(String response) {
try{
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(AppConfig.JSON_ARRAY);
JSONObject Data = result.getJSONObject(0);
Name = Data.getString(AppConfig.first_name)+" "+Data.getString(AppConfig.last_name);
contact = Data.getString(AppConfig.contact);
email = Data.getString(AppConfig.email);
}catch (JSONException e){
e.printStackTrace();
}
Toast.makeText(this, Name+" "+contact+" "+email, Toast.LENGTH_LONG).show();
}
private void prepareListData() {
listDataHeader = new ArrayList<>();
listDataChild = new HashMap<String,List<String>>();
//Adding header to expandable list view
listDataHeader.add("Information");
listDataHeader.add("Contact");
//Adding child data
List<String> Information = new ArrayList<>();
Information.add("Name : "+Name);
List<String> contact = new ArrayList<>();
contact.add("Contact Number : "+ contact);
contact.add("Email : "+ email);
//Header child data
listDataChild.put(listDataHeader.get(0),Information);
listDataChild.put(listDataHeader.get(1),contact);
}
private void dynamicToolbarColor() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.images);
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
collapsingToolbarLayout.setContentScrimColor(palette.getMutedColor(ContextCompat.getColor(getApplicationContext(),R.color.colorPrimary)));
collapsingToolbarLayout.setStatusBarScrimColor(palette.getMutedColor(ContextCompat.getColor(getApplicationContext(),R.color.colorPrimaryDark)));
}
});
}
private void toolBarTextAppearance() {
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.collapsedappbar);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.expandedappbar);
}
@Override
public void onSupportActionModeStarted(ActionMode mode) {
}
@Override
public void onSupportActionModeFinished(ActionMode mode) {
}
@Nullable
@Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
return null;
}
}
如何异步更新UI?
答案 0 :(得分:0)
我如何平行更新ui?
您需要组织&#34;流程&#34;更好。
例如。 (缩进意思是方法调用)
onCreate
fetchEverything
addToRequestQueue(stringRequest)
onResponse
showJSON
collapsingToolbarLayout.setTitle(Name) // <---- Move this here
prepareListData() // <---- Move this here
expListView = new ExpandableListAdapter...
expListView.setAdapter(listAdapter) // <--- Move this here
Update UI
// ^^^ This section your question?
目前您的代码可能正在执行此操作
onCreate
fetchEverything
addToRequestQueue(stringRequest)
collapsingToolbarLayout.setTitle(Name)
prepareListData()
expListView = new ExpandableListAdapter...
expListView.setAdapter(listAdapter)
...
(some unknown future point)
onResponse
showJSON
Toast.makeText
所以,换句话说,showJSON
(以及你的排球数据)直到 之后才进入你的工具栏和适配器。