Check in which Activity I am

1

I have this Button in my Adapter of RecyclerView and when I click on it you should update my RecyclerView with the method ActualizarRecyclerView

    holder.delete_nota.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mDatabase.deleteNotas(singleNotas.getId());

            ActualizaRecyclerView();
            ActualizaRecyclerViewDiaria();
            ActualizaRecyclerViewSemanal();
            ActualizaRecyclerViewMensual();
            ActualizaRecyclerViewFavorito();

        }
    });

The problem is that each method is used for a different Activity , for example:

ActualizarRecyclerView = MainActivity

ActualizarRecyclerView = MainActivity2

...

Therefore, I need something similar to check in that Activity I'm clicking on the button to execute that method, but I receive null not being in that Activity . p>

My idea has been that, to verify in which Activity I am in order to execute this method, but if you have a better solution, welcome it!

Thanks:)

EDITO1:

When I click on cardview_item what it does is show an icon in CardView , and if I click again hide it.

Adapter

@Override
public void onBindViewHolder(final NotasViewHolder holder, final int position) {
    final Notas singleNotas = listNotas.get(position);

    holder.icon_fav.setVisibility(singleNotas.getFavorito().equals("")? View.GONE:View.VISIBLE);

    holder.cardview_item.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mDatabase.FavNotas(singleNotas.getId());
            notifyDataSetChanged();
           //ActualizaRecyclerView();

        }
    });
}

The FavNotas method is in my Sqlite :

   public void FavNotas(int id)
    {
        SQLiteDatabase db = this.getReadableDatabase();
        String sql = "SELECT " + COLUMN_FAVORITO + " FROM " + TABLE_NOTAS + " WHERE " + COLUMN_ID + " = ?";
        Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(id)});
        cursor.moveToFirst();

        String val = "";
        ContentValues values = new ContentValues();
        values.put(COLUMN_FAVORITO, val);
        db.update(TABLE_NOTAS, values, COLUMN_ID + " = ?", new String[] { String.valueOf(id)});
        if (!cursor.getString(cursor.getColumnIndex(COLUMN_FAVORITO)).equals("favorito"))
        {
            val = "favorito";
            ContentValues values2 = new ContentValues();
            values2.put(COLUMN_FAVORITO, val);
            db.update(TABLE_NOTAS, values2, COLUMN_ID + " = ?", new String[] { String.valueOf(id)});
        }

        ContentValues values1 = new ContentValues();
        values1.put(COLUMN_FAVORITO, val);
        db.update(TABLE_NOTAS, values1, COLUMN_ID + " = ?", new String[] { String.valueOf(id)});
    }

The problem is that when I click the action is performed but my RecyclerView is not updated and the change is not seen until I go out and enter the Activity , therefore what I came up with was to create a method to update RecyclerView :

 public static void ActualizaRecyclerView() {
        mDatabase = new SqliteDatabase(ContextyMetodos.getAppContext());

        allNotas = (ArrayList<Notas>) mDatabase.listNotas();

        if (allNotas.size() > 0) {
            RecyclerVacio.setVisibility(View.GONE);
            recyclerview_notas.setVisibility(View.VISIBLE);
            mAdapter = new NotasAdapter(ContextyMetodos.getAppContext(), allNotas);
            mAdapter_d.filter("");
            recyclerview_notas.setAdapter(mAdapter);
        } else {
            RecyclerVacio.setVisibility(View.VISIBLE);
            recyclerview_notas.setVisibility(View.GONE);
        }
    }

I have the same RecyclerView in different Activity but with mAdapter_filter(""); different, so when I click on cardview_item , depending on what Activity I am, I need you to use a ActualizarRecyclerView or other, I do not know how to make a "universal" one so that it is updated in any Activity but respecting the mAdapter_filter("")

EDITO2:

This is what happens, at first it seems to be fine, but when I click several times:

Adapter

public class NotasAdapter extends RecyclerView.Adapter<NotasAdapter.NotasViewHolder> {
    private Context context;
    public static ArrayList<Notas> listNotas;
    private SqliteDatabase mDatabase;

    public NotasAdapter(Context context, List<Notas> listNotas) {
        this.context = context;
        Collections.reverse(listNotas);
        this.listNotas = (ArrayList<Notas>) listNotas;
        mDatabase = new SqliteDatabase(context);
    }

    @Override
    public NotasViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.notas_list_adapter, parent, false);
        return new NotasViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final NotasViewHolder holder, final int position) {
        final Notas singleNotas = listNotas.get(position);

        holder.icon_fav.setVisibility(singleNotas.getFavorito().equals("")? View.GONE:View.VISIBLE);

        holder.edit_nota.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent= new Intent(context, EditarNota.class);
                intent.putExtra("editNotas", singleNotas);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);

            }
        });
        holder.delete_nota.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //delete row from database
                mDatabase.deleteNotas(singleNotas.getId());
             //
                listNotas.remove(position);
                //una ves removido el item, se notifican los cambios para que se actualize la lista
                notifyDataSetChanged();
                /*((Activity)context).finish();
                context.startActivity(((Activity) context).getIntent());*/

            }
        });
        holder.cardview_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDatabase.FavNotas(singleNotas.getId());

                //actualizas el icono, o los elementos que quieras
                if(singleNotas.getFavorito().equals("")){
                    singleNotas.setFavorito("favorito");
                }
                else{
                    singleNotas.setFavorito("");
                }
                //usas este para notificar cambios solo en una posicion de la lista
                notifyItemChanged(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return listNotas.size();
    }
    
asked by UserNameYo 25.06.2017 в 14:35
source

2 answers

2

My proposal is that when you click on the button to delete an element, delete it as you already do in your SQL, and instead of "redoing" the list with the new data by calling the method according to its name, delete it from your List the item and notify the changes in this way:

holder.delete_nota.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        mDatabase.deleteNotas(singleNotas.getId());

        // position es la posición del item seleccionado
        // tuLista es la lista que datos que estas usando en el adapter
        tuLista.remove(position);
        //una ves removido el item, se notifican los cambios para que se actualize la lista
        notifyDataSetChanged();

    }
});

EDIT:

If clicking on an element you want to change something, just do it as if it were a normal object, it is not necessary to notify the changes. Why does the method work for you? Because you build a new adapter and assign it. Why does not it work to notify changes on your adapter? When you do it, the adapter class is using the same data it contained, so if it has not been altered in List it will reload what it had when it was opened.

@Override
public void onBindViewHolder(final NotasViewHolder holder, final int position) {
    final Notas singleNotas = listNotas.get(position);

    holder.icon_fav.setVisibility(singleNotas.getFavorito().equals("")? View.GONE:View.VISIBLE);

    holder.cardview_item.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mDatabase.FavNotas(singleNotas.getId());

            //actualizas el icono, o los elementos que quieras
            if(holder.icon_fav.getVisibility() == View.VISIBLE){
                holder.icon_fav.setVisibility(View.GONE);
            }
            else{
                holder.icon_fav.setVisibility(View.INVISIBLE);
            }
        }
    });
}

Another way to do it if you want to do it by notifying the changes:

@Override
public void onBindViewHolder(NotasViewHolder holder, final int position) {
    final Notas singleNotas = listNotas.get(position);

    holder.icon_fav.setVisibility(singleNotas.getFavorito().equals("")? View.GONE:View.VISIBLE);

    holder.cardview_item.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mDatabase.FavNotas(singleNotas.getId());

            //actualizas el icono, o los elementos que quieras
            if(singleNotas.getFavorito().equals("")){
                singleNotas.setFavorito("favorito");
            }
            else{
                singleNotas.setFavorito("");
            }
            //usas este para notificar cambios solo en una posicion de la lista
            notifyItemChanged(position);
        }
    });
}

EDIT 2: Surely you will have the problem that when you scroll, the elements previously altered, return to the initial position. This is because Android, destroys the elements that are not visible to recreate. To solve that, in your Adapter overwrite the method getItemViewType :

@Override
public int getItemViewType(int position) {
    return position;
}
    
answered by 25.06.2017 / 16:07
source
3

If you're in oncreate :

this.getClass().getSimpleName();

But it specifies the name of the class instead of this .

Another option is to declare an object with a variable with its methods get and set , and set the value in the activity, so that this value will be that of the last active activity.

    
answered by 25.06.2017 в 15:39