Listview by selecting position

Asked

Viewed 655 times

0

I have a code I need him to select the item by its position... In case he is already selecting but he selects randomly.

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  @Override
  public boolean onItemLongClick(final AdapterView<?> adapterView, final View view, final int position, long l) {
    AlertDialog.Builder builder = new AlertDialog.Builder(BuscaActivity.this);
    builder.setTitle("Deseja inserir na lista?");
    builder.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface arg0, int arg1) {
        TextView codigointerno = (TextView) findViewById(R.id.codigointerno);
        String interno = codigointerno.getText().toString();
        TextView codigoean = (TextView) findViewById(R.id.codigo_ean);
        String ean = codigoean.getText().toString();
        ListaProduto produto = new ListaProduto();
        produto.setCodigoproduto(interno);
        produto.setCodigoean(ean);
        produto.setUclogin(getSharedPreferences(PREFS_USER, Context.MODE_PRIVATE).getString("PrefUser", ""));
        loadJsonLista(produto);
      }
    });



    public class ListaAdapter  extends ArrayAdapter<Produto> {
private Context context;
private List<Produto> values;

public ListaAdapter(Context context, List<Produto> values) {
    super(context, R.layout.lista_item, values);

    this.context = context;
    this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;

    if (row == null) {
        LayoutInflater inflater =
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.lista_item, parent, false);
    }
    TextView interno = (TextView)row.findViewById(R.id.codigointerno);
    TextView ean = (TextView) row.findViewById(R.id.codigo_ean);
    TextView completa = (TextView) row.findViewById(R.id.descricao_completa);
    TextView reduzida = (TextView) row.findViewById(R.id.descricao_reduzida);

    Produto item = values.get(position);

    String message_interno = item.getCodigointerno();
    String message_ean = item.getCodigoean();
    String message_completa = item.getDescricaocompleta();
    String message_reduzida = item.getDescricaoreduzida();

    interno.setText(message_interno);
    ean.setText(message_ean);
    completa.setText(message_completa);
    reduzida.setText(message_reduzida);

    return row;
}

}

          public class ListaProduto implements Serializable {

private static final long serialVersionUID = -2161110911377686463L;

@SerializedName("uclogin")
String uclogin;

@SerializedName("codigoproduto")
String codigoproduto;

@SerializedName("codigoean")
String codigoean;

public String getUclogin() {
    return uclogin;
}

public void setUclogin(String uclogin) {
    this.uclogin = uclogin;
}

public String getCodigoproduto() {
    return codigoproduto;
}

public void setCodigoproduto(String codigoproduto) {
    this.codigoproduto = codigoproduto;
}

public String getCodigoean() {
    return codigoean;
}

public void setCodigoean(String codigoean) {
    this.codigoean = codigoean;
}

}

            public class Produto {
 public String codigointerno;
public String codigoean;
public String descricaocompleta;
private String descricaoreduzida;
private double precovenda;
private double precocusto;
private double estoque;
private double margem;


public String getCodigointerno() {
    return codigointerno;
}

public void setCodigointerno(String codigointerno) {
    this.codigointerno = codigointerno;
}

public String getCodigoean() {
    return codigoean;
}

public void setCodigoean(String codigoean) {
    this.codigoean = codigoean;
}

public String getDescricaocompleta() {
    return descricaocompleta;
}

public void setDescricaocompleta(String descricaocompleta) {
    this.descricaocompleta = descricaocompleta;
}

public String getDescricaoreduzida() {
    return descricaoreduzida;
}

public void setDescricaoreduzida(String descricaoreduzida) {
    this.descricaoreduzida = descricaoreduzida;
}

public double getPrecovenda() {
    return precovenda;
}

public void setPrecovenda(double precovenda) {
    this.precovenda = precovenda;
}

public double getPrecocusto() {
    return precocusto;
}

public void setPrecocusto(double precocusto) {
    this.precocusto = precocusto;
}

public double getEstoque() {
    return estoque;
}

public void setEstoque(double estoque) {
    this.estoque = estoque;
}

public double getMargem() {
    return margem;
}

public void setMargem(double margem) {
    this.margem = margem;
}

}

           public void loadJsonLista(ListaProduto produto){

    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("http://"+getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString("PrefHost", "") +":8080/FazendaWebservice/webresources/fazenda/")
            .addConverterFactory(GsonConverterFactory.create());

    Retrofit retrofit = builder.build();

    ListaProdutosClient client = retrofit.create(ListaProdutosClient.class);
    Call<ListaProduto> call = client.reposForUser(produto);

    call.enqueue(new Callback<ListaProduto>() {
        @Override
        public void onResponse(Call<ListaProduto> call, Response<ListaProduto> response) {
                ListaProduto produtos = response.body();
                Toast.makeText(getApplicationContext(), "Código: " + produtos.getCodigoproduto(), Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onFailure(Call<ListaProduto> call, Throwable t) {
            Toast.makeText(BuscaActivity.this, "Erro ao criar lista", Toast.LENGTH_SHORT).show();
        }
    });
}

1 answer

0

You created an array list right ? you need to take the current position in the arraylist that is being populated, so you get the data you need, it would look more or less like this, when you are instantiating the class can do this way:

ListaProduto produto = arrayList.get(position);

This way you recover the data according to the position in the method call

public boolean onItemLongClick(final AdapterView<?> adapterView, final View view, final int position, long l)

How I installed my Adapter:

adapter = new MinhaClasseAdapter(AtualActivity.this, arrayList);
listView.setAdapter(adapter);
  • Dude I’m using retrofit, I didn’t use arraylist, I just used an Adapter.

  • How did you plant your Adapter? If you were using arrayList you would put it as the second parameter of your Adapter and then your listview would receive this Adapter, see how I would do and show how you instanced your Adapter. att,

  • I will edit the question and put the Adapter for you to see.

  • Blz, your Adapter class gets a List<Product>(in the constructor method) when it is instantiated in another class, so whenever you instantiate that class you pass that list, in that list the get(position) method you will use to get the clicked position of the list, what I wanted was for you to show me this code of your current class : Adapter = new Minhaclasseadapter(Atualactivity.this, list); in this example the variable list has the information you need, can show me how you prompted your Listadapter class within the main Longclick method class?

  • Dude I’m gonna put everything I’m wearing in editing

  • Well, you should have specified that you’re using the title retrofit or at least the description when you opened this topic, you didn’t put that information in the tags and so I thought you were using arraylist or something like, I don’t know anything about retrofit, what I could help I helped, I advise you to ask a new question and this time specify your situation better, so someone more prepared can help you! Hugs !

Show 1 more comment

Browser other questions tagged

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