Tengo problema al actualizar el listview
, yo agrego el nuevo contacto todo bien (me queda guardado en la base de datos SQLite) pero no me actualiza el nuevo registro en el listview
.
El código que tengo es:
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;
}