Reputation: 876
I have my data base and I show some data in a ListView. I want to pass on data to another activity when the user click at the item in ListView
long idProjeto;
ProjetosDAO pDAO;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_projetos);
ListView lv = (ListView)findViewById(R.id.lvListaProjetos);
final ListaProjetosAdapter adapter = new ListaProjetosAdapter(this);
pDAO = new ProjetosDAO(this);
pDAO.open();
Cursor cursor = pDAO.consultaParaListaprojetos();
String data = "";
String cliente = "";
String tipoProjeto = "";
String status = "";
if(cursor != null && cursor.moveToFirst()){
do{
data = cursor.getString(0);
cliente = cursor.getString(1);
tipoProjeto = cursor.getString(2);
status = cursor.getString(3);
idProjeto = Long.parseLong(cursor.getString(4));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
Date date = sdf.parse(data);
data = new SimpleDateFormat("dd/MM/yyyy HH:mm").format(date);
} catch (ParseException e) {
e.printStackTrace();
}
adapter.addDadosListaprojetos(data, cliente, tipoProjeto, status, idProjeto);
}while(cursor.moveToNext());
cursor.close();
pDAO.close();
}
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(ListaProjetosActivity.this, ListaCheckinsActivity.class);
intent.putExtra("idProjeto", idProjeto);
startActivity(intent);
}
});
}
public void novoProjeto(View v) {
Intent intent = new Intent(this, NovoProjetoActivity.class);
startActivity(intent);
}
}
I want to pass this idProjeto to another activity, but its passing only the data from the last item. Each loop from the while, the variable get other value. How do get the right value?
Upvotes: 0
Views: 609
Reputation: 990
Build a Projeto class in order to model your objects:
public class Projeto {
private long idProjeto;
private String data;
private String cliente;
private String tipoProjeto;
private String status;
public Projeto() {
super();
}
public Projeto(long idProjeto, String data, String cliente, String tipoProjeto, String status) {
super();
this.idProjeto = idProjeto;
this.data = data;
this.cliente = cliente;
this.tipoProjeto = tipoProjeto;
this.status = status;
}
public long getIdProjeto() {
return idProjeto;
}
public void setIdProjeto(long idProjeto) {
this.idProjeto = idProjeto;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String getCliente() {
return cliente;
}
public void setCliente(String cliente) {
this.cliente = cliente;
}
public String getTipoProjeto() {
return tipoProjeto;
}
public void setTipoProjeto(String tipoProjeto) {
this.tipoProjeto = tipoProjeto;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
Then in your adapter take the idProjeto field as the id, so it will be passed to the onitemclicklistener as the id parameter:
public class ProjetoAdapter extends ArrayAdapter<Projeto> {
public ProjetoAdapter(Context context, int resource,
int textViewResourceId, List<Projeto> objects) {
super(context, resource, textViewResourceId, objects);
}
public ProjetoAdapter(Context context, int resource,
int textViewResourceId, Projeto[] objects) {
super(context, resource, textViewResourceId, objects);
}
public ProjetoAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
}
public ProjetoAdapter(Context context, int resource, List<Projeto> objects) {
super(context, resource, objects);
}
public ProjetoAdapter(Context context, int resource, Projeto[] objects) {
super(context, resource, objects);
}
public ProjetoAdapter(Context context, int resource) {
super(context, resource);
}
@Override
public long getItemId(int position) {
return this.getItem(position).getIdProjeto();
}
// Other methods that you want to override
}
Upvotes: 0
Reputation: 44571
Of course the value will be overwritten with each iteration of the loop
. Instead of putting them into a long
variable put them into an ArrayList
. Then use the value of position
in onItemClick()
to retrieve that value from the ArrayList
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
idProjeto = arrayList.get(position); // assuming you create a member variable ArrayList<Long> arrayList = new ArrayList<Long>()
Intent intent = new Intent(ListaProjetosActivity.this, ListaCheckinsActivity.class);
intent.putExtra("idProjeto", idProjeto);
startActivity(intent);
Upvotes: 2