Update listview SwipeRefreshLayout

enter the description of the image here

Taking as startup I'm ending my app on android, and require updating my custom listview when I swipe down the screen.

I am implementing SwipeRefreshLayout only that I get stuck in the onRefresh method part and I don't know how to update my ListView, I clarify my listview I fill it from a webservices using the Asyntask class.

I leave the code my Layout and my activity

Layout..

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipeContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/clientes"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

</android.support.v4.widget.SwipeRefreshLayout>

Activity

public class Clientes extends AppCompatActivity implements SearchView.OnQueryTextListener{

    ProgressDialog dialog;
    ListView list;
    ArrayList<CXCPSaldoClienteProveedor> cliArrayList = new ArrayList<CXCPSaldoClienteProveedor>();
    ArrayList<String> searchlist;
    MyArrayAdapter adaptador;
    public static String O_Cliente = "O_Cliente";
    DetalleMenu O_DetalleMenu;
    TextView textView;
    SwipeRefreshLayout mSwipeRefreshLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clientes);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Intent intent = getIntent();
        O_DetalleMenu = (DetalleMenu) intent.getSerializableExtra("O_DetalleMenu");
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                adaptador.notifyDataSetChanged();
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });
        list = (ListView) findViewById(R.id.clientes);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                TextView idcli = (TextView) view.findViewById(R.id.clienid);
                Intent intent = new Intent(Clientes.this, Detalle_Cliente.class);
                intent.putExtra("O_Cliente", (CXCPSaldoClienteProveedor) ((TextView) view.findViewById(R.id.clienid)).getTag());
                intent.putExtra("O_DetalleMenu", (DetalleMenu) O_DetalleMenu);
                startActivity(intent);
            }
        });

        AsynClien task = new AsynClien();
        //Call execute
        task.execute();
    }

    @Override
    public boolean onCreateOptionsMenu(android.view.Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_cliente, menu);

        final MenuItem searchItem = menu.findItem(R.id.action_search);
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        //permite modificar el hint que el EditText muestra por defecto
        searchView.setQueryHint(getText(R.string.search));
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (TextUtils.isEmpty(newText)) {
                    adaptador.filter("");
                    list.clearTextFilter();
                } else {
                    adaptador.filter(newText);
                }
                return true;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }

    private class AsynClien extends AsyncTask<String, ArrayList, ArrayList> {


        @Override
        protected ArrayList doInBackground(String... params) {
            cliArrayList = webService.Clientes(O_DetalleMenu.getDocumento());
            return null;
        }

        @Override
        //Make Progress Bar visible
        protected void onPreExecute() {

            dialog = new ProgressDialog(Clientes.this);
            dialog.setIndeterminate(false);
            dialog.setMessage("Cargando Clientes...");
            dialog.setCancelable(false);
            dialog.show();

        }

        //Once WebService returns response
        @Override
        protected void onPostExecute(ArrayList arrayList) {
            super.onPostExecute(arrayList);
            if (cliArrayList.size() != 0) {
                dialog.dismiss();

                adaptador = new MyArrayAdapter(Clientes.this, cliArrayList);///* no se usa your_array_list
                //list.setAdapter(adaptador);

                ListView listView = (ListView) findViewById(R.id.clientes);
                listView.setAdapter(adaptador);


            } else {
                dialog.dismiss();
                finish();
            }
        }
    }




    public class MyArrayAdapter extends ArrayAdapter<CXCPSaldoClienteProveedor>{

        private List<CXCPSaldoClienteProveedor> searchList;

        public MyArrayAdapter(Context context, ArrayList<CXCPSaldoClienteProveedor> ArrayClientes)
        {
            super(context, 0, ArrayClientes);
            this.searchList = new ArrayList<>();
            this.searchList.addAll(cliArrayList);
        }


        @Override
        public int getCount() {
            return cliArrayList.size();
        }



        public View getView(int position, View convertView, ViewGroup parent) {

            RecyclerView.ViewHolder holder;

            CXCPSaldoClienteProveedor O_Cliente = getItem(position);

            // Check if an existing view is being reused, otherwise inflate the view
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_cliente, parent, false);
            }


            //Obteniendo instancias de los text views
            TextView nombre = (TextView) convertView.findViewById(R.id.nombrecli);
            TextView saldov = (TextView) convertView.findViewById(R.id.txtsaldov);
            TextView saldot = (TextView) convertView.findViewById(R.id.txtsaldot);
            TextView idcli = (TextView) convertView.findViewById(R.id.clienid);


            //INICIALIZAR FORMAT
            DecimalFormat numberFormat = new DecimalFormat("###,##0.00");

            nombre.setText(O_Cliente.getClienteDescripcion());
            nombre.setTag(O_Cliente);

            saldov.setText(numberFormat.format(O_Cliente.getSaldoVencido()));
            saldot.setText(numberFormat.format(O_Cliente.getSaldo()));

            idcli.setText(String.valueOf(O_Cliente.getCliente()));
            idcli.setTag(O_Cliente);

            // Se almacena en settag el objeto Cliente
            convertView.setTag(O_Cliente);

            //Devolver al ListView la fila creada
            return convertView;
        }

        public void filter(String newText) {
            newText = newText.toLowerCase(Locale.getDefault());
            cliArrayList.clear();
            if (newText.length() == 0) {
                cliArrayList.addAll(searchList);
            } else {
                for (CXCPSaldoClienteProveedor s : searchList) {
                    if (s.toLowerCase(Locale.getDefault()).contains(newText)) {
                        cliArrayList.add(s);
                    }
                }
            }
            notifyDataSetChanged();
        }
    }


} 

Refresh Code

mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);
mSwipeRefreshLayout.setDistanceToTriggerSync(20);// in dips
mSwipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        new AsynClien().execute();
    }
});
 2
Author: Hugo Rodriguez, 2016-05-19

1 answers

When performing a "pull to refresh", you must run your Asynctask

mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                 new AsynClien().execute();
                 //adaptador.notifyDataSetChanged();
                //mSwipeRefreshLayout.setRefreshing(false);
            }
        });

And inside the onPostExecute() method of your Asynctask, update the adapter with the new data, adaptador.notifyDataSetChanged(); which is what you have apparently already done.

For the" indicator", which is actually a circular progressBar to appear or disappear the method is used setRefreshing()

Notifies the widget that the update status has changed. No call when the update is being made.

To force it to appear at start:

SwipeRefreshLayout.setRefreshing(true);

To force it to disappear at the end:

SwipeRefreshLayout.setRefreshing(false);
 2
Author: Jorgesys, 2016-05-19 16:28:45