Refresh Listview Android with SQL Server

Asked

Viewed 61 times

1

I’m making an application where I have two activities: One that will show users who have been registered in a ListView, and another that will make it possible to execute the CRUD.

I have a problem with how to do a refresh every time I insert, update or delete a data in Activity 2 (the one that registers). How should I proceed in this case?

Below the code of the Filllist class that fills the 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);

    }

}
  • Is it an external (sqlite) or internal bank? Where is the part you insert?! or delete?

  • @white. It is an external database (Sql Server), I will edit the post with the insert class.

  • Could upload the list to onResume from the first Activity.

  • @Andréozawa then I create a method onResume and put the part to fill the list?

  • Yeah. It’s actually gonna be about writing protected void onResume() { . It is a quick way to implement. If you prefer not to reload the list, you can return the changed object, and then make the manipulation of the list.

  • Could you show me what the code would look like? I still don’t understand what you said, excuse me

Show 1 more comment

1 answer

0


onResume is executed after onStart, that may come after onCreate or onRestart, depends on the state of the activity. I recommend a look at Activity’s life cycle. It would be something like:

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

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

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

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

  • So this method will always run when I go back to Activity? (I’ve seen a part of the Cycle of an Activity.) Forgive me, I’m still learning about Android and Java.

  • Thank you very much André, it worked out here! I had another logic in mind, but this was more practical ;)

  • Yes, it will always perform. Quiet, the important thing is to always be learning. Good @Matheusarduino, I hope to have helped, if helped could set as answer? It may be useful to others too.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.