加载列表时显示进度条

Sun*_*nny 2 android android-listview progress-bar

好的我现在承认我是新手使用进度条我从不使用它但现在我需要使用它我有一个活动(主)和一个菜单,可以启动6个新的活动.从这些活动中有一个活动,它在ListView中加载数据需要3-4秒才能加载.这个活动解析json并将数据传递给另一个活动.如果用户单击此活动的菜单选项,我将如何显示进度条,并在加载列表时将其消失.

这是活动

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     final Intent intent=new Intent(this ,GetLatAndLng.class);
    setContentView(R.layout.listplaceholder);
    //ProgressBar pb=(ProgressBar)findViewById(R.id.progressbar);
    LocationManager locationManager;
    String context=Context.LOCATION_SERVICE;
    locationManager=(LocationManager)getSystemService(context);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    String provider = locationManager.getBestProvider(criteria, true);
    Location location = locationManager.getLastKnownLocation(provider);

    final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
        updateWithNewLocation(location);
        }
        public void onProviderDisabled(String provider){
        updateWithNewLocation(null);
        }
        public void onProviderEnabled(String provider){ }
        public void onStatusChanged(String provider, int status,
        Bundle extras){ }
        };
    updateWithNewLocation(location);
    locationManager.requestLocationUpdates(provider, 2000, 10,
            locationListener);
    double geoLat = location.getLatitute();
    double geoLng = location.getLongitude();
         Bundle b=new Bundle();
    //pb.setVisibility(View.VISIBLE);
    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

    JSONObject json = JSONFunction.getJSONfromURL(getUrl());
    Log.v(TAG, "got the json"); 
    try{
        JSONArray  JArray = json.getJSONArray("results");
           Log.v(TAG, "getting results");
        for(int i=0;i<JArray.length();i++){                     
            HashMap<String, String> map = new HashMap<String, String>();    
            JSONObject e = JArray.getJSONObject(i);
            JSONObject location1=e.getJSONObject("geometry").getJSONObject("location");
            latitude[i]=location1.getDouble("lat");
            longitude[i]=location1.getDouble("lng");
            reference[i]=e.getString("reference");
            Log.v(TAG, reference[i]);
            distance[i]=GetLatAndLng.gps2m(geoLat, geoLng,latitude[i] ,longitude[i]); 
            map.put("id",  String.valueOf(i));
            map.put("name", "" + e.getString("name"));
            map.put("vicinity", "Address " +  e.getString("vicinity")+" "+"Disance:"+distance[i]);

            mylist.add(map);                
        }           
    }catch(JSONException e)        {
         Log.e("log_tag", "Error parsing data "+e.toString());
    }
//   pb.setVisibility(View.GONE);
    b.putStringArray("key", reference);
    intent.putExtras(b);
    Log.v(TAG, ""+reference); 
    ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.listview, 
                    new String[] { "name", "vicinity", }, 
                    new int[] { R.id.item_title, R.id.item_subtitle });

    setListAdapter(adapter);
    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);  
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {        
            @SuppressWarnings("unchecked")
            HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
           Toast.makeText(JsonExampleActivity.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
            intent.putExtra("clickedid",position);
            startActivity(intent);
        }
    });
}
public void updateWithNewLocation(Location location2) {
    if(location2!=null) {
          double geoLat = location2.getLatitude();
            double geoLng = location2.getLongitude();
    }
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!!

Ars*_*war 6

用于AsyncTask在显示加载指示符的同时在后台加载数据.在AsyncTask's doInBackground方法中,处理JSON或任何花费时间的事情.

public class HeavyWorker extends AsyncTask < String , Context , Void > {

    private ProgressDialog      progressDialog ;
    private Context             targetCtx ;

    public HeavyWorker ( Context context ) {
        this.targetCtx = context ;
        this.needToShow = true;
        progressDialog = new ProgressDialog ( targetCtx ) ;
        progressDialog.setCancelable ( false ) ;
        progressDialog.setMessage ( "Retrieving data..." ) ;
        progressDialog.setTitle ( "Please wait" ) ;
        progressDialog.setIndeterminate ( true ) ;
    }

    @ Override
    protected void onPreExecute ( ) {
        progressDialog.show ( ) ;
    }

    @ Override
    protected Void doInBackground ( String ... params ) {
      // Do Your WORK here

       return null ;
    }

    @ Override
    protected void onPostExecute ( Void result ) {
        if(progressDialog != null && progressDialog.isShowing()){
            progressDialog.dismiss ( ) ;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在您的Activity中onCreate()执行AsyncTask

new HeavyWorker().execute();
Run Code Online (Sandbox Code Playgroud)