简体   繁体   中英

progress bar when downloading some data from parse.com

I am pretty new in Android so my question may sound very common. I am trying to make appear a progress bar when downloading some data from parse.com and make it disappear when the data has been finished downloading and has been presented in a listView. I looked at the documentation in Parse but it is not very clear how I can use their tools. I understand I have to use some thread in order to make the work in background and not kipping busy the UI.. Please see bellow my code and help me understand how I can manage..

ListViewsAllReports

public class ListViewsAllReports extends Activity implements OnClickListener{

    Button aReportsBackButton;
    ListView aReports;
    lvAdapterAllReports lvAdapterAllReports;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_views_all_reports);

        Parse.initialize(this, "hm9VwSWl4hyrI1ZbkrvEXXkwdvXSxZLZYbheDaFs", "4BmzC3n9qrt0OhceQJ8yzp3iKrIU7rdsAhf2FSQW");

        setUpUI();

    }//onCreate

    public void setUpUI(){
        aReportsBackButton=(Button)findViewById(R.id.AllReportsBackButton);
        aReportsBackButton.setOnClickListener(this);

        aReports=(ListView)findViewById(R.id.AllReportsLV);
        lvAdapterAllReports = new lvAdapterAllReports(getApplicationContext(), "Animal");
        aReports.setAdapter(lvAdapterAllReports);
        registerForContextMenu(aReports);
    }//void setUpUI()

lvAdapterAllReports

public class lvAdapterAllReports extends ParseQueryAdapter<ParseObject>  {

    private Vector<Animal> animalList ;

    public lvAdapterAllReports(Context context, String className){
        super(context, className);

        animalList = new Vector<Animal>();

        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Animal");
        query.findInBackground(new FindCallback<ParseObject>() {

            @Override
            public void done(List<ParseObject> objects, ParseException e) {
                // TODO Auto-generated method stub
                if (e == null) {
                    for (int i = 0; i < objects.size(); i++) {
                        Animal animal = new Animal();
                        animal.setValueFromParse(objects.get(i));
                        animalList.add(animal);
                    }//for
                }//if
            }//void done()
        });//anonymous class

    }//constructor

    public View getItemView(ParseObject object, View v, ViewGroup parent) {
        LinearLayout linearLayout;
        if (v == null) {
            linearLayout = (LinearLayout)View.inflate(getContext(), R.layout.mycasesviews, null);
        }
        else{
            linearLayout = (LinearLayout)v;
        }
        super.getItemView(object, v, parent);
        final Animal a = new Animal();
        a.setValueFromParse(object);


        ((TextView)linearLayout.findViewById(R.id.myCaseViewLostOrFound)).setText(a.getmLostOrFound());
        ((TextView)linearLayout.findViewById(R.id.myCaseViewAnimalType)).setText(a.getmType());
        ((TextView)linearLayout.findViewById(R.id.myCaseViewDate)).setText(a.getmDate()+"");
        ((Button)linearLayout.findViewById(R.id.myCaseViewContactButton)).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(getContext(), ContactOwner.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("UserId", a.getmOwnerId());
                getContext().startActivity(intent);

            }
        });


        final ImageView img = (ImageView)linearLayout.findViewById(R.id.myCaseViewImage);

        ParseFile fileImage = (ParseFile)object.getParseFile("Image");
        if (fileImage != null) {

            fileImage.getDataInBackground(new GetDataCallback() {

                @Override
                public void done(byte[] data, ParseException e) {
                    if (e==null) {
                        Bitmap resultImage = BitmapFactory.decodeByteArray(data, 0, data.length);
                        img.setImageBitmap(resultImage);                    
                    }//if
                }//done
            });//anonymous class
        }//if fileImage

        return linearLayout;
    }//getItemView




}//class

You can implement the onLoaded method to achieve that:

lvAdapterAllReports.addOnQueryLoadListener(new OnQueryLoadListener<ParseObject>() {
   public void onLoading() {
   } 
   public void onLoaded(List<ParseObject> objects, ParseException e) {
      // Dismiss your Progress Bar here
   }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM