Problems with Listview Adapter

Asked

Viewed 33 times

0

I own an Activity that has a ListView. The items of ListView I built it myself. I put a EditText and I need to do an event setOnClickListener in it. But the right thing would be to do this in the main Activity and not in the Adapter class. I have declared the Adpter in the main Activity, but how do I trigger this event?

Code of the Adapter:

public View getView(int position, View convertView, ViewGroup parent) {
    final ExclusaoItem item = itens.get(position);
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.item_exclusao,null);

    TextView ref = view.findViewById(R.id.txt_ref_exclusao);
    ref.setText(String.valueOf(item.getProduto()));

    TextView tamanho = view.findViewById(R.id.txt_tamanho_exclusao);
    tamanho.setText(String.valueOf(item.getTamanho()));

    TextView situacao = view.findViewById(R.id.txt_situacao_exclusao);
    if(item.getEstoque()==1){
        situacao.setVisibility(View.VISIBLE);
        situacao.setText("Estoque");
    }

    final EditText codBarrasPainel = view.findViewById(R.id.edt_painel_produto);

I need to do setOnClickListener in codBarrasPainel that is at the end of the code.

1 answer

0


You can create the event as an interface in the Adapter without any action and in the Activity you instance it and place the desired action.

Adapter:

//resto da classe



private ObjetoAdapter.ObjetoOnClickListener objetoOnClickListener;

/**
* interface para clique
*/
public  interface ObjetoOnClickListener{
  public  void onClickObjeto(View view, int idx);
}

at the getView event():

if (objetoOnClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    objetoOnClickListener.onClickObjeto(holder.itemView, position);
                }
            });
        }

//Activity:

/**
     * evento aplicado na interface OnGridMesaClick
     * @return ObjetoAdapter.ObjetoOnClickListener
     */
    private ObjetoAdapter.ObjetoOnClickListener onClickObjeto(){
        return  new ObjetoAdapter.ObjetoOnClickListener(){
            @Override
            public void onClickObjeto(View view, int idx) {
                onGridObjetoClick.onClick(objetos.get(idx));
            }
        };
    }
  • I did a quick draft, might have some mistakes

  • 1

    Will I have to use this huge code just for a setOnClicklistener in a field of the list. I did not understand very well, but I will try. I thought it would be simpler. =(

  • It is that in Adapter you will create an interface, you can search about them and try to reduce in the way that suits you most.

Browser other questions tagged

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