0

My recyclerview displays 2 textviews and 1 imageview. Inside the adapter's onbindviewholder, I can set the 2 textview's texts just fine, but when I change the colors, they go invisible.

Also, when I delete the recyclerview item, then "undo" it, it'll then show the color changes. What am I doing wrong?

I suspect that it has something to do with one of the notify...() methods but when I tried putting one of them inside the onbindviewholder, the app will crash on startup.

public class MemoListAdapter extends RecyclerView.Adapter<MemoListAdapter.ViewHolder> {

private CardView cv;
private List<Memo> mMemos;
private Context mContext;
public List<Memo> memosToRemove;

//pass in the list of notes to display
public MemoListAdapter(List<Memo> memos, Context context) {
    mMemos = memos;
    mContext = context;

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View rowView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_memo_list, parent, false);
    ViewHolder viewHolder = new ViewHolder(rowView, mContext, mMemos);


    return viewHolder;
}

//@param holder & position - identifies the Note we are working with
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {

    holder.memoTitle.setText(mMemos.get(position).getTitle());
    holder.memoContent.setText(mMemos.get(position).getContent());
    holder.memoIcon.setColorFilter(mMemos.get(position).getNotificationColor());
    holder.memoTitle.setTextColor(mMemos.get(position).getNotificationColor());
}

Here's the undo method that will correctly display the changes to the color

 public void onItemRemove(final RecyclerView.ViewHolder viewHolder, final RecyclerView mRecyclerView) {

    final int adapterPosition = viewHolder.getAdapterPosition();
    final int memoID = mMemos.get(adapterPosition).getId();
    final DatabaseHandler db = new DatabaseHandler(mContext);
    final Memo memo = db.getMemo(memoID);

    mMemos.remove(adapterPosition);
    db.deleteMemo(memo);
    notifyItemRemoved(adapterPosition);

    Snackbar snackbar = Snackbar
            .make(mRecyclerView, mContext.getString(R.string.note_deleted), Snackbar.LENGTH_LONG)
            .setAction(R.string.undo_string, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mMemos.add(adapterPosition, memo);
                    notifyItemInserted(adapterPosition);
                    mRecyclerView.scrollToPosition(adapterPosition);
                    db.addMemo(memo);
                }
            });

[IMAGE] When app opens

I then swipe away the note then quickly press undo

[IMAGE] After I press the undo delete on the snackbar

snlvtr
  • 21
  • 1
  • 3
  • are you changing color programmatially – Jitesh Mohite Nov 15 '16 at 06:18
  • 2
    @snlvtr tell me the value of "mMemos.get(position).getNotificationColor()" – Anant Shah Nov 15 '16 at 06:19
  • 1
    @snlvtr Try holder.memoTitle.setTextColor(Color.RED) for a test case? And check is it working or not – Raghavendra Nov 15 '16 at 06:21
  • @AnantShah the value I get is 6, which is weird. I use the getNotificationColor() in another class and it works fine. – snlvtr Nov 15 '16 at 06:21
  • @Raghavendra just tried it now, it works. – snlvtr Nov 15 '16 at 06:23
  • @snlvtr u better decide which color u have to use and set that color then. – Raghavendra Nov 15 '16 at 06:24
  • @Raghavendra the thing is though, I want the color to be what the user chooses. The data which is stored in my database – snlvtr Nov 15 '16 at 06:25
  • @snlvtr how u r getting which color user has choosen? – Raghavendra Nov 15 '16 at 06:27
  • @Raghavendra I have another class which is basically a note editor. They have an option there to choose a color from a predefined list. That color is saved in a database along with other note details such as time created, title of the note, contents, time edited, etc. In that same activity, I use the getNotificationColor to change the statusbar and layout color, which works fine. – snlvtr Nov 15 '16 at 06:31
  • should I edit in a picture? Will that help? – snlvtr Nov 15 '16 at 06:33
  • @snlvtr u said u r changing status bar color can u post that line how u r changing that? – Raghavendra Nov 15 '16 at 06:34
  • @Raghavendra Oh I just used this code snippet http://stackoverflow.com/questions/4928772/android-color-darker – snlvtr Nov 15 '16 at 06:49
  • @snlvtr but u said u have used mMemos.get(position).getNotificationColor() to set status bar color – Raghavendra Nov 15 '16 at 07:04
  • @Raghavendra yes, substitute int color = getColor(); to int color = defaultColor....... the defaultColor is as such: defaultColor = mMemo.getNotificationColor(); – snlvtr Nov 15 '16 at 07:14

2 Answers2

3
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    if(position == selected_item) {
        holder.title.setTextColor(Color.parseColor("#00aaff"));
        holder.imgViewIcon.setBackgroundResource(R.drawable.ic_circle);
    } else {
        holder.title.setTextColor(Color.parseColor("#00000")); //actually you should set to the normal text color
        holder.imgViewIcon.setBackgroundResource(0);
    }
    NavDrawerItem current = data.get(position);
    holder.title.setText(current.getTitle());
    holder.imgViewIcon.setImageResource(current.getIcon());


}

Edit: removed links (was supposed to edit into OP)

snlvtr
  • 21
  • 1
  • 3
0

Managed to solve the problem by editing the onBindViewHolder to this:

 @Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    Memo m = mMemos.get(position);
    int id = m.getId();
    DatabaseHandler db = new DatabaseHandler(mContext);
    Memo memo = db.getMemo(id);
    int color = memo.getNotificationColor();

    holder.memoTitle.setText(mMemos.get(position).getTitle());
    holder.memoContent.setText(mMemos.get(position).getContent());
    holder.memoIcon.setColorFilter(color);
    holder.memoTitle.setTextColor(color);

Basically I had to actually take the item or "Memo" in my case directly from the database instead of relying on taking the memo based on @param position

snlvtr
  • 21
  • 1
  • 3