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?
– viana
@white. It is an external database (Sql Server), I will edit the post with the insert class.
– Matheus Arduino
Could upload the list to onResume from the first Activity.
– André Ozawa
@Andréozawa then I create a method onResume and put the part to fill the list?
– Matheus Arduino
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.– André Ozawa
Could you show me what the code would look like? I still don’t understand what you said, excuse me
– Matheus Arduino