How to replace ProgressDialog (obsolete) with ProgressBar in retrofit?

6

Since ProgressDialog is "deprecated" I am trying to replace it with a ProgressBar, I have an activity with a list and I am trying to show a ProgressBar before the list is displayed after an http query with retrofit.

I have used "progressBar.setVisibility (View.Visible / Gone / Invisible)" but they are displayed once the list is finished loading.

public class MisSolicitudes extends AppCompatActivity {

    private Retrofit retrofit;
    private String token = null;
    private ListView lvSolicitudes;
    private static final String TAG = "retrofit";
    private boolean clickable_item=true;
    private ProgressDialog progressDialog;
    Context context=this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mis_solicitudes);
        clickable_item=true;

        progressDialog = new ProgressDialog(context);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setTitle("Cargando");
        progressDialog.setMessage("Consultando...");
        progressDialog.show();

        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        this.setTitle("Solicitudes");

        SharedPreferences preferences = getSharedPreferences("preferencias", Context.MODE_PRIVATE);
        token = preferences.getString("token", "null");

        retrofit = new Retrofit.Builder().baseUrl("xxxxxxxxxxxxxxxxxxxxxxxxx")
                .addConverterFactory(GsonConverterFactory.create()).build();

        lvSolicitudes = (ListView) findViewById(R.id.misSolicitudes);
        obtenerMisSolicitudes();
    }

    private void obtenerMisSolicitudes(){

        Map<String,String> map = new HashMap<>();
        map.put("Content-Type", "application/json;charset=utf-8");
        map.put("Authorization", "Bearer "+token);

        RestService serviceSolicitudes = retrofit.create(RestService.class);
        Call<List<SolicitudesDto>> misSolicituresRespuesta = serviceSolicitudes.comprobarSolicitudes(map);

        misSolicituresRespuesta.enqueue(new Callback<List<SolicitudesDto>>() {
            @Override
            public void onResponse(Call<List<SolicitudesDto>> call, Response<List<SolicitudesDto>> response) {
                if(response.isSuccessful()){

                    final List<SolicitudesDto> misSolicitudes = response.body();

                    if(misSolicitudes.size()==0){
                        lvSolicitudes.setEmptyView(findViewById(R.id.emptyListView_));
                    }

                    SolicitudesListAdapter adapter = new SolicitudesListAdapter(getApplicationContext(), misSolicitudes);
                    lvSolicitudes.setAdapter(adapter);
                    progressDialog.dismiss();

                }else{
                    lvSolicitudes.setEmptyView(findViewById(R.id.emptyListView_));
                }
            }

            @Override
            public void onFailure(Call<List<SolicitudesDto>> call, Throwable t) {

                lvSolicitudes.setEmptyView(findViewById(R.id.emptyListView_));
                progressDialog.dismiss();

            }
        });

    }
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light"
    tools:context=".....MisSolicitudes">

    <ListView
        android:id="@+id/misSolicitudes"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/cardview_light_background"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/emptyListView_"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        android:text="No se han encontrado"
        android:textAlignment="center">
    </TextView>

</android.support.constraint.ConstraintLayout>
    
asked by Canet 04.05.2018 в 10:18
source

1 answer

0

According to your code, I would start by assigning an id to the main container, which is the ConstraintLayout , which is where the ProgressBar would be displayed, for example assign as id container :

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

    ...
    ...


    android:id="@+id/container">

Then delete the references of the ProgressDialog , and add the ProgressBar , review the comments in code:

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mis_solicitudes);
        clickable_item=true;

        /*progressDialog = new ProgressDialog(context);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setTitle("Cargando");
        progressDialog.setMessage("Consultando...");
        progressDialog.show();*/

      /**************** ProgressBar *********************/

      ConstraintLayout cl = (ConstraintLayout) findViewById(R.id.container);
        progressBar = new ProgressBar(getApplicationContext(), null, android.R.attr.progressBarStyleLarge);
        // Crea layout parameters para el ProgressBar
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        progressBar.setLayoutParams(lp);
        //Define como indeterminado.
        progressBar.setIndeterminate(true);
        // Agrega el ProgressBar al Layout
        cl.addView(progressBar);

      /*****************************************/



        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        this.setTitle("Solicitudes");

        SharedPreferences preferences = getSharedPreferences("preferencias", Context.MODE_PRIVATE);
        token = preferences.getString("token", "null");

        retrofit = new Retrofit.Builder().baseUrl("xxxxxxxxxxxxxxxxxxxxxxxxx")
                .addConverterFactory(GsonConverterFactory.create()).build();

        lvSolicitudes = (ListView) findViewById(R.id.misSolicitudes);
        obtenerMisSolicitudes();
    }

This would show up in your Activity :

You can even change the style to show it horizontally,

progressBar = new ProgressBar(getApplicationContext(), null, android.R.attr.progressBarStyleHorizontal);

similar to Android applications:

In the case where you remove the ProgressDialog when receiving the response to the request,

progressDialog.dismiss();

You can change the visibility of ProgressBar to invisible:

 progressBar.setVisibility(View.INVISIBLE);

applied to your code:

misSolicituresRespuesta.enqueue(new Callback<List<SolicitudesDto>>() {
    @Override
    public void onResponse(Call<List<SolicitudesDto>> call, Response<List<SolicitudesDto>> response) {
        if(response.isSuccessful()){

            final List<SolicitudesDto> misSolicitudes = response.body();

            if(misSolicitudes.size()==0){
                lvSolicitudes.setEmptyView(findViewById(R.id.emptyListView_));
            }

            SolicitudesListAdapter adapter = new SolicitudesListAdapter(getApplicationContext(), misSolicitudes);
            lvSolicitudes.setAdapter(adapter);
            //progressDialog.dismiss();
              progressBar.setVisibility(View.INVISIBLE);

        }else{
            lvSolicitudes.setEmptyView(findViewById(R.id.emptyListView_));
        }
    }

    @Override
    public void onFailure(Call<List<SolicitudesDto>> call, Throwable t) {

        lvSolicitudes.setEmptyView(findViewById(R.id.emptyListView_));
        //progressDialog.dismiss();
        progressBar.setVisibility(View.INVISIBLE);

    }
});

Here is an example that you could also implement which shows the progress by means of a AsyncTask .

How to display a ProgressBar while getting a response from the server?

    
answered by 05.05.2018 в 00:08