Refresh ListView Android with SQL Server

1

I'm doing an application where I have two Activities: One that will show users that have been registered in a ListView , and another that will allow to run CRUD.

I have a difficulty in how to refresh whenever I insert, update or delete a data in Activity 2 (the one that does the registration). How should I proceed in this case?

Below is the code for the FillList class that fills in ListView :

    public class FillList extends AsyncTask <String,String,String> {
    String z="";

    List<Map<String, String>> prolist = new ArrayList<Map<String, String>>();

    @Override protected void onPreExecute()
    {
        progbar2.setVisibility(View.VISIBLE);
    }

    @Override protected  void onPostExecute(String r){
        progbar2.setVisibility(View.GONE);
        //Toast.makeText(MegaPermanentes_Usuarios.this, r, Toast.LENGTH_SHORT).show();

        String[] from = {"A", "B"};
        int[] views = {R.id.lblproname, R.id.lblproend};
        final SimpleAdapter ADA = new SimpleAdapter(MegaPermanentes_Usuarios.this, prolist, R.layout.lsttemplate, from, views);
        listPro.setAdapter(ADA);

    }

    @Override
    protected String doInBackground(String... params) {

        try{
            Connection con = connectionClass.connectionclass();
            if(con == null){
                z = "Conexão falhou";
            }else
                {
                String query = "select nome,endereco from usuarios";
                PreparedStatement ps = con.prepareStatement(query);
                ResultSet rs = ps.executeQuery();

                while(rs.next()){
                    Map<String, String> datanum = new HashMap<String, String>();
                    datanum.put("A", rs.getString ("Nome"));
                    datanum.put("B", rs.getString("Endereco"));
                    prolist.add(datanum);
                }
            }
        }catch (Exception ex)
        {
            z = "Error Retrieving Data";
        }
        return z;
    }
}

The insertion part of the other Activity, follows below:

    public class AddInfo extends AsyncTask<String, Void, String>{


    Boolean isSucess = false;
    String infoName = editName.getText().toString();
    String infoDocu = editDocument.getText().toString();

    @Override
    protected  void onPreExecute()
    {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected String doInBackground(String... params) {
        String z = "";
        if(infoName.trim().equals("") || infoDocu.trim().equals(""))
        {
            Toast.makeText(MegaPermanentes.this, "Por favor digite um nome e um documento", Toast.LENGTH_SHORT);
        }
        else{
            try{
                Connection con = connectionClass.connectionclass();
                if (con == null){
                    Toast.makeText(MegaPermanentes.this, "Verifique sua conexão", Toast.LENGTH_SHORT);
                }
                else
                {
                    String query = "insert into usuarios (nome,endereco) values ('" + infoName + "','" +infoDocu + "')";
                    PreparedStatement preparedStatement = con.prepareStatement(query);
                    preparedStatement.executeUpdate();
                    z = "Cadastro inserido com sucesso";
                }
            }catch( Exception ex){
                Toast.makeText(MegaPermanentes.this, "ex" , Toast.LENGTH_LONG);
            }
        }

        return z;
    }

    @Override
    protected void onPostExecute(String result)
    {

        progressBar.setVisibility(View.GONE);
        Toast.makeText(MegaPermanentes.this, result, Toast.LENGTH_SHORT);

    }

}
    
asked by anonymous 28.08.2017 / 21:30

1 answer

0

onResume executes after onStart , which can come after onCreate or onRestart , depends on the state of the activity. I recommend taking a look at activity lifecycle . It would look something like:

public class PrimeiraActivity extends AppCompatActivity {
    //... code

    @Override
    protected void onResume() {
        super.onResume();

        // carregar lista. Ex.: fillListAsyncTask.execute();
    }
}

Considering that when inserting, updating or deleting a data in Activity 2, it is being finalized.

    
29.08.2017 / 15:40