Problem updating Listview when adding item

0

I have a problem updating the listview , I add the new contact all right (it is saved in the SQLite database) but it does not update the new record in listview .

The code I have is:

public class ContactosDetalle extends Activity  {
    private static final int REQUEST_CALL = 1;

    ConnectionClass connectionClass;
    private DBHelper helper;
    private List<Contactos> phoneList = new ArrayList<Contactos>();
    private ContactosAdapter adapter2;
    ListView lv2;
    private RadioButton rb, rb2;
    private EditText et2;
    private EditText et3;
    private Button bt10;
    private Button bt11;
    int iduser;
    Intent callinten;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contactos);

        connectionClass = new ConnectionClass();
        helper = new DBHelper(this);
        lv2 = (ListView) findViewById(R.id.listview2);
        rb = (RadioButton) findViewById(R.id.radioButton);
        rb2 = (RadioButton) findViewById(R.id.radioButton2);
        et2 = (EditText) findViewById(R.id.editText2);
        et3 = (EditText) findViewById(R.id.editText3);
        bt10 = (Button) findViewById(R.id.button10);
        bt11 = (Button) findViewById(R.id.button11);

        Intent intentc = getIntent();
        Bundle extrasc = intentc.getExtras();
        iduser = extrasc.getInt("idcli", 0);

        phoneList = helper.buscarContacto();
        adapter2 = new ContactosAdapter(this, phoneList);
        lv2.setAdapter(adapter2);

        bt11.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {

                AgregarContacto Ag = new AgregarContacto();
                Ag.execute();

            }
        });
    }

    public class AgregarContacto extends AsyncTask<String,String,String> {

        Boolean isSuccess = false;
        String z = "";
        String tipodato = "";

        String telefono = et2.getText().toString();
        String referencia = et3.getText().toString();
        int id;

        @Override
        protected void onPostExecute(String s) {
            Toast.makeText(ContactosDetalle.this, s, Toast.LENGTH_SHORT).show();
            if (isSuccess) {
               adapter2.notifyDataSetChanged();
                et2.setText("");
                et3.setText("");
            }
        }

        @Override
        protected String doInBackground(String... strings) {
            if (telefono.trim().equals("")) {
                z = "Ingresar Telefono";
            } else {
                try {
                    Connection con = connectionClass.CONNN();
                    if (con == null) {
                        z = "Error en Coneccion con Base de Datos";
                    } else {
                        String queryidnot = "SELECT MAX(Id)FROM ClientesContacto";
                        Statement stmtid = con.createStatement();
                        ResultSet rsid = stmtid.executeQuery(queryidnot);

                        if (rsid.next()) {
                            id = rsid.getInt(1) + 1;
                        }

                        if (rb.isChecked()){
                            tipodato = "Fijo";
                        }else{
                            tipodato = "Movil";
                        }

                        Date dt = new Date();
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                        String formatDate = df.format(dt);
                        String queryTel = "INSERT INTO [ClientesContacto] ([Id], [CodCliente], [TipoDato], [Telefono], [Referencia], [FechaIngreso], [OrdenPrio], [Vigente]) VALUES (? ,?, ?, ?, ?, ?,?,?)";
                        PreparedStatement psc = con.prepareStatement(queryTel);
                        psc.setInt(1, id);
                        psc.setInt(2, iduser);
                        psc.setString(3, tipodato);
                        psc.setString(4, telefono);
                        psc.setString(5, referencia);
                        psc.setDate(6, java.sql.Date.valueOf(formatDate));
                        psc.setInt(7, 0);
                        psc.setBoolean(8, true);
                        psc.executeUpdate();   //ResultSet rsc = executeQuery();

                        psc.close();
                        z = "Contacto Guardado";
                        helper.addContactos(id,iduser,tipodato,telefono,referencia,formatDate,0);
                        isSuccess = true;
                        con.close();
                    }

                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            return z;
        }
    }
}


public List<Contactos> buscarContacto() {
        SQLiteDatabase db = getReadableDatabase();
        List<Contactos> lista_contacto = new ArrayList<Contactos>();
        String[] valores_recuperar = {"id", "codCli", "tipoDato", "telefono", "referencia", "fechaIngreso","ordenPri"};

        Cursor c = db.query("contactos", valores_recuperar, null, null, null, null, null,null);

        if(c != null && c.getCount()!=0) {
            c.moveToFirst();
            do {
                Contactos contactos = new Contactos(c.getInt(0),c.getInt(1), c.getString(2), c.getString(3), c.getString(4), c.getString(5), c.getInt(6));
                lista_contacto.add(contactos);
            } while (c.moveToNext());
        }

        db.close();
        c.close();
        return lista_contacto;
    }
    
asked by Carlosd 14.04.2018 в 02:23
source

2 answers

0

At no time are you sending the new information to adapter2 , in your adapter you could create a setDatos(List<Contactos> l); once you have that you could just do adapter2.notifyDataSetChanged(); . You are notifying your adapter about the change, but the adapter never receives that change.

    
answered by 14.04.2018 / 17:28
source
1

You should include what your DBHelper Class does, maybe you are generating a new instance of List when you call the method: helper.buscarContacto (); so the adapter2.notifyDataSetChanged () method will not work, I would suggest that you create a globally unique list and add that new contact to it if the adapter2.notifyDataSetChanged () function works, another option is to do setAdapter again in your onPostExecute ().

    
answered by 14.04.2018 в 02:46