0

I'm trying to view a progress bar in my Android App that follows the filling of the database with some data and when it's complete the progress bar should view "100%" or just that the loading of the data is complete. I would use this Progress bar in my app.

While here is my class with all the algorithms with which i get data from a HTML site and put them in to SQLite. Do you have any suggestion on how can i implement a progress bar with it?

public class articoli extends AppCompatActivity {


String htmlresultart;
Integer DaDoveParto = 0;
Integer DoveMiFermo = 0;
Integer QuanteRighe = 0;
Integer QuantiCampi = 0;
String appCAMPOart = "";
String[] appBODYart = new String[9];
DataBaseHandler myDB;

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

    myDB = DataBaseHandler.getInstance(this);
    myDB.delete();
    getHTMLArticoli();

}

private void getHTMLArticoli(){

    Ion.with(getApplicationContext())
            .load("WEPPAGEIP")
            .asString()
            .setCallback(new FutureCallback<String>() {
                @SuppressLint("SetTextI18n")
                @Override
                public void onCompleted(Exception e, String result) {
                    htmlresultart = result;
                    htmlresultart = htmlresultart.replace("</td>", "\n");
                    getBodyArticoli();
                }
            });

}

  private void getBodyArticoli(){

    DaDoveParto = Integer.valueOf(String.valueOf(htmlresultart.indexOf("TBLCRP")));
    DoveMiFermo = Integer.valueOf(String.valueOf(htmlresultart.indexOf("</form>")));

    if(DaDoveParto == 0){
        Toast.makeText(this,"NESSUN DATO TROVATO",Toast.LENGTH_SHORT).show();
    }else
    {
        Integer i;
        Integer j;
        Integer CONTACAMPO = 0;
        for( i = DaDoveParto ; i <= DoveMiFermo ; i++){
            if( htmlresultart.substring(i, i + 4).equals("<td>")){
                i += 4;
                for (j = i; j <= DoveMiFermo ; j++){
                    if(htmlresultart.substring(j, j + 1).equals("\n")){

                        appBODYart[CONTACAMPO] = htmlresultart.substring(i, i + (j - i));

                        if(appBODYart[CONTACAMPO].equals("(null)")){
                            appBODYart[CONTACAMPO] = "";
                        }
                        CONTACAMPO += 1;

                        if(CONTACAMPO.equals(QuantiCampi)){
                            CONTACAMPO = 0;
                            myDB.insertArtServer(appBODYart[0], appBODYart[1], appBODYart[2], appBODYart[3], appBODYart[4], appBODYart[5],
                                    appBODYart[6], appBODYart[7], appBODYart[8]);
                        }
                        break;
                    }
                }
            }

        }

    }

}

}

1 Answers1

0

Actually you need a ProgressBar to displaying progress for downloading your data, not for inserting theme into database.

As i see you are using ion library for downloading your data. ion has ability to do that. look here (search for progress)

However if you want to use a ProgressBar for inserting data into database, you have to use a callback and put your percentage in it in your database handler and implement it in your activity.

Seyyed
  • 1,526
  • 3
  • 20
  • 30
  • Actually i don't need a ProgressBar to displaying progress for downloading my data because i'm just downloading some HTML text so that take just some mills to do it, while the action that take more time is that where i populate my DB with that data because i'm formatting the HTML text i've got like by deleting some tags from it and searching for some Strings in getHTMLBody function. Do you have any examples for callback method to implement in DB? –  May 31 '18 at 08:16
  • Ok, so you can use second solution. – Seyyed May 31 '18 at 08:18
  • hm maybe you're right sorry i'm new in Android ill try now to make the progress bar for downloading the page and ill try to check if the time of completing progressbar will be +- same for other operation. –  May 31 '18 at 08:21
  • Take a look at this: https://stackoverflow.com/questions/18854060/how-to-implement-progressbar-while-loading-data (specially third answer) – Seyyed May 31 '18 at 08:24
  • and https://stackoverflow.com/questions/18054720/what-is-callback-in-android (Second and third answer) – Seyyed May 31 '18 at 08:31
  • You were right actually the application take most time by taking the HTML code that filling the database with it, so also just by following the [link](https://github.com/koush/ion) you've posted before helped a lot. Thank you. –  May 31 '18 at 08:41
  • Damn actually that also manage the DB filling because the ProgressBar get 100% only when all the methods in OnComplese of ion library are completed that was what i what searching for thank you another time –  May 31 '18 at 08:51