1

especially older programmers. I need some help because I am using a ListView and a Custom Adapter in a commercial application that requires a order menu and in this menu some Edit Texts field. I am searching for some way to return a custom object with the values that the user placed in the fields, I tried with listView.getItemAtPosition(X) but this return a generic object of Object class and I don't know how to turn this Object into my custom class. Above goes my custom adapter:

    package com.mobile.pedido.pedidomobile.model.adapter;

    import android.content.Context;
    import android.graphics.Color;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    import com.mobile.pedido.pedidomobile.*;
    import com.mobile.pedido.pedidomobile.dao.local.ItemPedidoTempDAO;
    import com.mobile.pedido.pedidomobile.model.ItemPedidoModelo;
    import java.util.List;

    /**
     * Created by MATEUS on 11/02/2017.
     */
    public class ProdutoListamgePrecoAdapter extends BaseAdapter {

    private Context context;
    private List<ItemPedidoModelo> lista;
    int cod_pedido;
    TextView tv_sequencia, tv_nome_item;
    EditText et_quantidade, et_valor;
    Button btn_excluir;


    public ProdutoListamgePrecoAdapter(Context context, List<ItemPedidoModelo> lista, int cod_pedido) {
        this.context = context;
        this.lista = lista;
        this.cod_pedido = cod_pedido;
    }

    @Override
    public int getCount() {
        return lista.size();
    }

    @Override
    public Object getItem(int position) {
        return lista.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ItemPedidoModelo itemPedidoModelo = lista.get(position);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.pedidoitempedido_itemprecoadapter, null);

         tv_sequencia = (TextView) layout.findViewById(R.id.pedidoitempedido_itemprecoadapter_sequencia);
         tv_nome_item = (TextView) layout.findViewById(R.id.pedidoitempedido_listagemadapter_nome);
         et_quantidade = (EditText) layout.findViewById(R.id.pedidoitempedido_itemprecoadapter_quantidade);
         et_valor = (EditText) layout.findViewById(R.id.pedidoitempedido_itemprecoadapter_preco);
         btn_excluir = (Button) layout.findViewById(R.id.pedidoitempedido_itemprecoadapter_excluir);

        tv_sequencia.setText(Integer.toString(itemPedidoModelo.getSequencia()));
        tv_nome_item.setText(itemPedidoModelo.getNome_item());

        et_quantidade.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    lista.get(position).setQuantidade(et_quantidade.getText().toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        et_valor.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {



            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    lista.get(position).setPreco(s.toString());
                    Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
                } catch (NumberFormatException e) {
                    Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });

        btn_excluir.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (itemPedidoModelo.isAtivo()) {
                        ItemPedidoTempDAO iptdao = new ItemPedidoTempDAO();
                        iptdao.excluirItemPedidoTemp(context, cod_pedido, itemPedidoModelo.getSequencia());
                        btn_excluir.setText("EXCLUIDO");
                        btn_excluir.setEnabled(false);
                        btn_excluir.setTextColor(Color.RED);
                        tv_sequencia.setTextColor(Color.RED);
                        tv_nome_item.setTextColor(Color.RED);
                        et_quantidade.setEnabled(false);
                        et_valor.setEnabled(false);
                        itemPedidoModelo.setAtivo(false);

                    } else {
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        });

        return layout;
    }
}

This is my custom object:

package com.mobile.pedido.pedidomobile.model;

import java.math.BigDecimal;

public class ItemPedidoModelo {

    private String cod_prod;
    private String cod_pedido;
    private String nome_item;
    private BigDecimal quantidade;
    private BigDecimal preco;
    private BigDecimal total_item;
    private int sequencia;
    private boolean ativo = true;

    public BigDecimal getTotal_item() {
        return total_item;
    }

    public void setTotal_item(BigDecimal total_item) {
        this.total_item = total_item;
    }

    public int getSequencia() {
        return sequencia;
    }

    public void setSequencia(int sequencia) {
        this.sequencia = sequencia;
    }

    public void setQuantidade(BigDecimal quantidade) {
        this.quantidade = quantidade;
    }

    public void setPreco(BigDecimal preco) {
        this.preco = preco;
    }

    public ItemPedidoModelo(){};

    public ItemPedidoModelo(String cod_prod, String cod_pedido, String nome_item, BigDecimal quantidade, BigDecimal preco, int sequencia) {
        super();
        this.cod_prod = cod_prod;
        this.cod_pedido = cod_pedido;
        this.nome_item = nome_item;
        this.quantidade = quantidade;
        this.preco = preco;
        this.sequencia = sequencia;

    }


    public String getNome_item() {
        return nome_item;
    }


    public void setNome_item(String nome_item) {
        this.nome_item = nome_item;
    }

    public String getCod_pedido() {
        return cod_pedido;
    }

    public void setCod_pedido(String cod_pedido) {
        this.cod_pedido = cod_pedido;
    }

    public String getCod_prod() {
        return cod_prod;
    }

    public void setCod_prod(String cod_prod) {
        this.cod_prod = cod_prod;
    }


    public BigDecimal getQuantidade() {
        return quantidade;
    }

    public void setQuantidade(String quantidade) {
          this.quantidade = new BigDecimal(quantidade);
    }

    public BigDecimal getPreco() {
        return preco;
    }

    public void setPreco(String valor) {
        this.preco= new BigDecimal(valor);       
    }

    public boolean isAtivo() {
        return ativo;
    }

    public void setAtivo(boolean ativo) {
        this.ativo = ativo;
    }

    @Override
    public String toString() {
        return "PedidoItemModelo \ncod_prod=" + cod_prod + "\n cod_pedido=" + cod_pedido + "\n nome_item=" + nome_item
                + "\n quantidade=" + quantidade + "\n preco=" + preco + "\n sequencia=" + sequencia + "]" +"\n\n\n";
    }
}
1
  • My intro should be "Hi everybody, especially olders programmers." :/ Commented Mar 2, 2017 at 1:38

1 Answer 1

1

I get it using the following code to acess a child of listView:

View listItem = listView.getChildAt(0);
TextView sequencia = (TextView) listItem.findViewById(R.id.pedidoitempedido_listagemadapter_nome);

I used the method "getChildAt" of listview and after this,I used findViewById with the TextView that i need to get the text value

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.