Calling GridView event in Activity

2

I have a GridView adapter and I have a ImageButton to delete the items. It is deleting correctly, however, every time an item is deleted I need to set the current quantity to a TextView that is in another Activity . I can not get the GridView event directly, because as it has other clickable components, your ringtone has been disabled:

Here is the code snippet where I exclude the item and try to pass the current amount of items to TextView 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();




            }
        });
    
asked by anonymous 04.03.2015 / 18:30

4 answers

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);
                        }
                    });
                }
            }

In the other class that calls and controls the GridView you declare the interface;

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

Hello,

I think one solution that might help you is to use the startActivityForResult method, you can go to the documentation for Android or give a read in this tutorial in en: link at the end the tutorial explains what it is and how to use the startActivityForResult .

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

    
14.03.2015 / 01:26
0

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

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

Good luck.

    
09.09.2016 / 07:07
0

You can open the GridView activity, delete the items, and then return the number of items to the TextView activity.

In the activity with GridView (I'll name GridViewActivity) you should call the setResult(int resultCode, Intent data) at shutdown. Replace 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 the activity that contains the TextView (I'll call it TextViewActivity), place the constant on the top that will be used to identify the GridViewActivity when it is finished:

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

When you start 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, put the code for finish() on save and on cancel use setResult(RESULT_CANCELED) .

    
23.01.2017 / 02:26