I have created a query using a ListView from a local database, I do not need to show all the information in the registry, only the basics, since I have also created an OnitemClickListener so that when you press a data of the existing ones in the ListView takes me to a new activity. The problem is that I do not know how to establish the data of the new activity This is the code:
Update: I have already solved the problem thanks to the help of the community, the code below is fully functional
This is the code of the query through the ListView
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.example.beeo.proyecto_final.constantes.Constante;
import com.example.beeo.proyecto_final.entidades.Cliente;
import java.util.ArrayList;
public class ConsultarActivity extends AppCompatActivity {
ListView datos;
ArrayList<String> lista_datos;
ArrayList<Cliente> lista_usuario;
Conexion con;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_consultar);
con = new Conexion(getApplicationContext(), "db_proyecto",null,1);
datos = (ListView)findViewById(R.id.lv_datos);
consultar_lista_datos();
ArrayAdapter adaptador = new ArrayAdapter(this, android.R.layout.simple_list_item_1,lista_datos);
datos.setAdapter(adaptador);
datos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Cliente es un constructor que tiene la informacion de las entidades de la base de datos asi como los get y set
Cliente idCliente = lista_usuario.get(position);
Intent actividad = new Intent(getApplicationContext(),DetalleActivity.class);
actividad.putExtra("id",idCliente);
startActivity(actividad);
}
});
}
private void consultar_lista_datos() {
SQLiteDatabase db = con.getReadableDatabase();
Cliente usuario = null;
lista_usuario = new ArrayList<Cliente>();
Cursor cursor = db.rawQuery("select * from "+Constante.tabla_cliente,null);
while (cursor.moveToNext()){
usuario = new Cliente();
usuario.setId(cursor.getInt(0));
usuario.setNombre(cursor.getString(1));
usuario.setApellido(cursor.getString(2));
usuario.setCedula(cursor.getString(3));
lista_usuario.add(usuario);
}
obtener_lista();
}
private void obtener_lista() {
lista_datos = new ArrayList<String>();
for (int i = 0; i<lista_usuario.size(); i++){
lista_datos.add(lista_usuario.get(i).getId()+" - "+lista_usuario.get(i).getNombre()+" - "+lista_usuario.get(i).getApellido()+" - "+lista_usuario.get(i).getCedula());
}
}
}
This is the code of the DetailActivity
Cliente id =(Cliente)getIntent().getExtras().getSerializable("id");
nombre.setText(id.getNombre());
EYE: I have two constructor methods, one empty which does not receive parameters to which I added Serializable implements, then it would be like this: public class Client implements Serializable and another one that if it receives the normal parameters for the database
Thank you very much for all the help