在我的主要活动中,我使用从我的服务器下载数据的方法初始化了一个类。
我希望在下载之前显示进度对话框。但它没有显示进度对话框。
我的主要活动:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
setFragmentManager(new MainActivityFragment());
Updater updater = new Updater(this,(ViewGroup) findViewById(R.id.fragment));
updater.check();
}
public void setFragmentManager(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id){
case R.id.action_settings:
return true;
case R.id.medi_calc_item:
setFragmentManager(new MediCalcFragment());
default:
return super.onOptionsItemSelected(item);
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
switch (id){
case R.id.nav_start:
setFragmentManager(new MainActivityFragment());
break;
case R.id.nav_medis:
setFragmentManager(new MediAbcFragment());
break;
case R.id.nav_bos:
setFragmentManager(new BosAbcFragment());
break;
default:
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
我的班级:
public class Updater {
private Context context;
private ViewGroup viewGroup;
private VersionsDAO versionsDAO;
private MediDAO mediDAO;
private RegioDAO regioDAO;
private HashMap<String,Integer> onlineVersions;
private HashMap<String,Integer> versions;
private ProgressDialog progressDialog;
public Updater(Context context, ViewGroup viewGroup) {
this.context = context;
this.versionsDAO = new VersionsDAO(context);
this.mediDAO = new MediDAO(context);
this.viewGroup = viewGroup;
this.onlineVersions = new HashMap<>();
this.regioDAO = new RegioDAO(context);
this.progressDialog = new ProgressDialog(context);
}
public void check(){
progressDialog.setMessage(context.getString(R.string.update_cities));
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setIndeterminate(true);
progressDialog.show();
RequestQueue queue = Volley.newRequestQueue(this.context);
String url ="http://resper.de/resperApi.php?cmd=deStadt";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jArray = new JSONArray(response);
for(int i = 0; i < jArray.length(); i++){
JSONObject jObject = jArray.getJSONObject(i);
regioDAO.insertDeStadt(jObject.getInt("id"), jObject.getString("name"), jObject.getInt("bl"), jObject.getInt("rb"), jObject.getInt("kreis"), jObject.getInt("lk"));
}
versionsDAO.updateVersion("deStadt", onlineVersions.get("deStadt"));
progressDialog.cancel();
}catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
serverNotAvailable();
}
});
queue.add(stringRequest);
progressDialog.cancel();
}
}
答案 0 :(得分:1)
从最后一行删除progressDialog.cancel();
。那是在queue.add(stringRequest);
之后再试一次。