Calling Gridview event in Activity

Asked

Viewed 197 times

1

I have a Gridview Adapter and in it I possess a ImageButton to delete items. It is deleting correctly, however, every time an item is deleted I need to set the current quantity in one TextView that is in another Activity. I can’t catch the event from GridView directly, because it has other clickable components, your touch has been disabled:

Follow the code snippet where I delete the item and try to pass the current amount of items to the TextView present in my Activity:

holder.imgDelete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                Integer position = (Integer)v.getTag();


                Produto_Valor newList[] = new Produto_Valor[values.length - 1]; 
                int count = 0;
                for (int i = 0; i < values.length; i++) {
                    if (values.length - 1 > 0) {
                        if (values[i] == values[1]) {                    
                        } else {
                            newList[count] = values[i];
                            count++;
                        }
                    }
                }
                values = newList; 
                notifyDataSetChanged();

                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.activity_pedidos, null);
            final TextView txtTotal = (TextView) view.findViewById(R.id.lbl_QtdeDados);



            new Thread (new Runnable(){
                @Override


                public void run(){

                    txtTotal.setText(String.valueOf(values.length));
                }
            }).start();




            }
        });
  • You have solved the problem Fabio, or you need some more information?

4 answers

0

Hello,

I think a solution that can help you is to use the method startActivityForResult, you can go in the Android documentation or read this tutorial in en: http://celeiroandroid.blogspot.com.br/2011/03/atividades.html, at the end of the tutorial is explained what is and how to use the startActivityForResult.

For your problem I believe you want two activities with a list of items and another that has this Textview with the amount of items removed. You will return the amount of items from one Activity to another, you will not need to use the inflate method for the Textview item in the Item Listing Activity.

  • Actually, I have an Activity and an Adapter, which extends from Basedapter. I implemented as you suggested, but I got the following error: java.lang.Runtimeexception: Unable to instantiate Activity Componentinfo{app.App/app.App.Gridviewadapter_itensrequest}: java.lang.Classcastexception: app.App.Gridviewadapter_itensrequest cannot be cast to android.app.Activity

0

You can create a role in your activity to update the TextView, for example atualizaTextView(String value). There the moment you call your imgDelete.setOnClickListener you execute the method this way:

((MinhaActivity)context).atualizaTextView(value);

Good luck.

0

You can create an interface in Gridviewadapter:

    public class SeuAdapter extends RecyclerView.Adapter<SeuAdapter.SeuViewHolder> {

       private SeuAdapter.SeuOnClickListener SeuOnClickListener;

        /**
         * construtor
         * @param context
         * @param seuOnClickListener
         */
        public MesaAdapter(Context context, SeuAdapter.SeuOnClickListener seuOnClickListener) {
            this.seuOnClickListener = seuOnClickListener;
        }

//interface
            public  interface GridViewOnClickListener{
                public  void onClickGridView(View view, int idx);
            }

    //no metodo onBindViewHolder:

        @Override
            public void onBindViewHolder(final MesaAdapter.MesaViewHolder holder, final int position) {

               ...

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

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

Ai in the other class calling and controlling the Gridview you make the interface statement;

    private SeuAdapter.SeuOnClickListener onClickSeu(){
        return  new SeuAdapter.SeuOnClickListener(){
            @Override
            public void onClickSeu(View view, int idx) {
                //execucao
            }
        };
    }

0

You can open the Gridview Activity, delete the items and then return the amount of items to the Textview Activity.

In Activity with Gridview (I will name Gridviewactivity) should call the method setResult(int resultCode, Intent data) at closure. Replace the finish() in Gridviewactivity as follows:

@Override
public void finish() {
    Intent data = new Intent();
    // insere a quantidade de itens no Intent
    data.putExtra("TamanhoDaLista", tamanhoDaLista);
    setResult(RESULT_OK, data);
    super.finish();
}

In Activity that contains Textview (I’ll call it Textviewactivity), put the constant at the top that will be used to identify Gridviewactivity when it is finished:

private static final int REQUEST_DELETE_ITENS = 0; // zero é só um exemplo

When starting Gridviewactivity, use:

startActivityForResult(REQUEST_CODE, intent);

Replace the method onActivityResult (int requestCode, int resultCode, Intent data):

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // verifica se o código de retorno é a GridViewActivity
    if (requestCode == REQUEST_DELETE_ITENS) {
        // verifica se a activity retornou com sucesso (talvez tenha um botão cancelar)
        if (resultCode == RESULT_OK) {
            // -1 se o valor não extiver presente no Intent (talvez esqueceu do putExtra?)
            textView.setText(data.getLongExtra("TamanhoDaLista", -1));
        }
    }
}

If in Griviewactivity you have a cancel and save button, enter the code for finish() on save and cancel use setResult(RESULT_CANCELED).

Browser other questions tagged

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