How to recover a product id by clicking on listview opening another Activity

Asked

Viewed 583 times

5

I am making an application for product consultation using a Webservice.

I do a search in WS and bring the product information I need. Then I pass this information to a Adapter create a ListView and then in your step this Adapter in Activity where I show registered products.

My question is: how do I make so that when clicked on the product I open the details of it in another Activity? I need to pass the product’s PK parameter to load the details of that product.

I’ve tried the codes below:

public void onItemClick(AdapterView<?> av, View v, int position, long id) {    
    int posicao = resultado.getSelectedItemPosition();
    intent  = new Intent(this, MostrarItemSelActivity.class);
    intent.putExtra("codigo",posicao);
    startActivity(intent);
    this.finish();
}

However, in this way, I regain the position of the item I clicked into ListView I cannot get the information inside that item like code, value, description etc.

Thanks in advance.

  • Could show how you ride this Adapter?

3 answers

0

There are some ways to "chat" between Activities, to simply send some information to other Activities, you can use the Bundle.

I believe that is answered in this another question

  • Thanks for the help. that answer also uses the Parcelable.

0

I didn’t quite understand the question, but first of all I strongly advise you to use a Recyclerview instead of a Listview. https://developer.android.com/training/material/lists-cards.html?hl=fr

You have an Arraylist of Product objects and you passed it as parameter of the Listview Adapter, so all you have to do is, within the function onItemClick, something like: Produto produto = resultado.get(position), whereas the result is an Arraylist containing Product instances. The Intent will change a little, because now you need to pass an object as parameter, I advise a reading on: https://github.com/johncarl81/parceler

  • Thanks for the reply. No more get(position) I do not get the position of the object in the Adapter? There is no such thing as 'Product product= result.get(position). get(attribute)'. Better explaining is the product object at position 0 and from that object I just need its code. or something like that kkkk. Anyway, I’ll read the link you left there.

0

You can use the Parcelable or Serializable.

Parcelable Example

package com.helpme.app;

import android.os.Parcel;
import android.os.Parcelable;

public class Produto implements Parcelable {

 int codigo;
 String nome;

 public Produto(int codigo, String nome){
     this.codigo = codigo;
     this.nome = nome;
 }

 public int getCodigo() {
     return codigo;
 }

 public void setCodigo(int codigo) {
     this.codigo = codigo;
 }

 public String getNome() {
     return nome;
 }

 public void setNome(String nome) {
     this.nome = nome;
 }

 // PARCELABLE
 protected Produto(Parcel in) {
     setCodigo(in.readInt());
     setNome(in.readString());
 }

 public static final Creator<Produto> CREATOR = new Creator<Produto>() {
     @Override
     public Produto createFromParcel(Parcel in) {
         return new Produto(in);
     }

     @Override
     public Produto[] newArray(int size) {
         return new Produto[size];
     }
 };

 @Override
 public int describeContents() {
     return 0;
 }

 @Override
 public void writeToParcel(Parcel dest, int flags) {
     dest.writeInt(getCodigo());
     dest.writeString(getNome());
 }
}

Passing the data via Intent

Produto produto = new Produto(1, "SmartPhone");
Intent it = new Intent(this, MostrarItemSelActivity.class);
it.putExtra("produto", produto);
startActivity(it);

Receiving the data (screen "Showitemselactivity")

Produto produto = getIntent().getExtras().getParcelable("produto");

Serializable Example

package com.helpme.app;

import java.io.Serializable;

public class Produto implements Serializable {

  int codigo;
  String nome;

  public Produto(int codigo, String nome){
      this.codigo = codigo;
      this.nome = nome;
  }

  public int getCodigo() {
      return codigo;
  }

  public void setCodigo(int codigo) {
      this.codigo = codigo;
  }

  public String getNome() {
      return nome;
  }

  public void setNome(String nome) {
      this.nome = nome;
  }
}

ArrayList<Produto> produtos = new ArrayList<>();
produtos.add(new Produto(1, "SmartPhone"));
produtos.add(new Produto(2, "TV 42"));

Intent it = new Intent(this, MostrarItemSelActivity.class);
// Se quiser passar a lista inteira
it.putExtra("produtos", produtos); 
// Se quiser passar apenas um objeto
it.putExtra("produto", produtos.get(0)); 
startActivity(it);

Retrieving the data

Produto produto = (Produto) getIntent().getSerializableExtra("produto");

ArrayList<Produto> produtos = (ArrayList<Produto>) getIntent().getSerializableExtra("produtos");

System.out.println("Produto: "+ produto.getNome());
System.out.println("Produtos: "+ produtos.get(0).getNome());

I advise you to use Recyclerview in place of Listview

  • Thanks for the help. I will implement and put here the results.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.