user3591436 user3591436 - 5 months ago 37
Android Question

Android notifyDataSetChanged not updating view on existing items?

New to android and having a hard time figuring out why notifyDataSetChanged(); isn't updating items that were removed from view when reinserting them. Working great when deleting items but not when restoring items back to view which were existing items in view. Any help is appreciated.

UsersAdapter.java

public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.UserViewHolder> {
private ArrayList<User> mDataSet;
private Context context;

private SQLiteDatabase db;

public UsersAdapter(ArrayList<User> mDataSet, Context context) {
this.context = context;
this.mDataSet = mDataSet;

createDatabase();

}

public void createDatabase(){
db=context.openOrCreateDatabase("Premfaces", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS faces(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR,url VARCHAR);");
}


@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_favorites2, parent, false);
UserViewHolder userViewHolder = new UserViewHolder(v);
return userViewHolder;
}

@Override
public void onBindViewHolder(UserViewHolder holder, int position) {


holder.name_entry.setText(mDataSet.get(position).getName());
holder.email_entry.setText(mDataSet.get(position).getEmail());
holder.icon_entry.setText(""+mDataSet.get(position).getName().charAt(0));

String name = mDataSet.get(position).getName();
String url = mDataSet.get(position).getEmail();

Glide.with(context)
.load(url)
.asBitmap()
.placeholder(R.drawable.ic_blank)
.error(R.drawable.ic_blank)
.into(holder.ivFavorites);
}

@Override
public int getItemCount() {
return mDataSet.size();
}

public class UserViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
TextView name_entry, email_entry, icon_entry;
ImageView ivFavorites;

UserViewHolder(View itemView) {
super(itemView);
cardView = (CardView) itemView.findViewById(R.id.user_layout);
name_entry = (TextView) itemView.findViewById(R.id.name_entry);
email_entry = (TextView) itemView.findViewById(R.id.email_entry);
icon_entry = (TextView) itemView.findViewById(R.id.icon_entry);
ivFavorites = (ImageView) itemView.findViewById(R.id.ivFavorites);

Typeface fontFamily2 = Typeface.createFromAsset(context.getAssets(), "fonts/fontawesome-webfont.ttf");
TextView fontFamilyText2 = (TextView) itemView.findViewById(R.id.fontAwesomeDirectLink);
fontFamilyText2.setTypeface(fontFamily2);
fontFamilyText2.setText("\uf0c1");
fontFamilyText2.setTextColor(ContextCompat.getColor(context, R.color.bg_screen3));
fontFamilyText2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
context = view.getContext();
final User mDataSet2 = mDataSet.get(getAdapterPosition());;
String url = mDataSet2.getEmail();
String name = mDataSet2.getName();
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Premface direct link", url);
clipboard.setPrimaryClip(clip);
Snackbar snackbar = Snackbar
.make(view, "Copied " + name + "!", Snackbar.LENGTH_LONG)
.setActionTextColor(context.getResources().getColor(R.color.bg_screen3))
.setAction("ADD IMG TAGS", new View.OnClickListener() {
@Override
public void onClick(View view) {
context = view.getContext();
final User mDataSet2 = mDataSet.get(getAdapterPosition());;
String url = mDataSet2.getEmail();
String name = mDataSet2.getName();
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Premface direct link with image tags", "[img]" + url + "[/img]");
clipboard.setPrimaryClip(clip);
Snackbar snackbar1 = Snackbar.make(view, "Added image tags!", Snackbar.LENGTH_SHORT);
snackbar1.show();
}
});

snackbar.show();
}
});

Typeface fontFamily3 = Typeface.createFromAsset(context.getAssets(), "fonts/fontawesome-webfont.ttf");
TextView fontFamilyText3 = (TextView) itemView.findViewById(R.id.fontAwesomeImageTagLink);
fontFamilyText3.setTypeface(fontFamily3);
fontFamilyText3.setText("\uf00d");
fontFamilyText3.setTextColor(ContextCompat.getColor(context, R.color.dot_dark_screen1));
fontFamilyText3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
context = view.getContext();
final User mDataSet3 = mDataSet.get(getAdapterPosition());;
String url = mDataSet3.getEmail();
String name = mDataSet3.getName();
String query = "DELETE FROM faces WHERE url = ('"+url+"');";
db.execSQL(query);
mDataSet.remove(getAdapterPosition());
notifyDataSetChanged();
Snackbar snackbar = Snackbar
.make(view, name + " Removed!", Snackbar.LENGTH_LONG)
.setActionTextColor(context.getResources().getColor(R.color.dot_dark_screen1))
.setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View view) {
context = view.getContext();
final User mDataSet3 = mDataSet.get(getAdapterPosition());;
String url = mDataSet3.getEmail();
String name = mDataSet3.getName();
String query = "INSERT INTO faces (name,url) VALUES('"+name+"', '"+url+"');";
db.execSQL(query);
Snackbar snackbar3 = Snackbar.make(view, name + " Favorited!", Snackbar.LENGTH_SHORT);
snackbar3.show();
}
});
snackbar.show();
}
});


Typeface fontFamily4 = Typeface.createFromAsset(context.getAssets(), "fonts/fontawesome-webfont.ttf");
TextView fontFamilyText4 = (TextView) itemView.findViewById(R.id.fontAwesomeShare);
fontFamilyText4.setTypeface(fontFamily4);
fontFamilyText4.setText("\uf1e0");
fontFamilyText4.setTextColor(ContextCompat.getColor(context, R.color.bg_screen3));
fontFamilyText4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
context = view.getContext();
final User mDataSet2 = mDataSet.get(getAdapterPosition());;
String url = mDataSet2.getEmail();
String name = mDataSet2.getName();
Drawable mDrawable = ivFavorites.getDrawable();
Bitmap mBitmap = ((BitmapDrawable) mDrawable).getBitmap();
Bitmap imageWithBG = Bitmap.createBitmap(600, 600, mBitmap.getConfig()); // Create another image the same size
imageWithBG.eraseColor(Color.WHITE); // set its background to white, or whatever color you want
Canvas canvas = new Canvas(imageWithBG); // create a canvas to draw on the new image
canvas.drawBitmap(mBitmap, 175, 100, null); // draw old image on the background
String path = MediaStore.Images.Media.insertImage(context.getContentResolver(),
imageWithBG, "Premface", null);
Uri uri = Uri.parse(path);
final Intent shareIntent = new Intent(Intent.ACTION_SEND); // THIS IS SLOW
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
context.startActivity(shareIntent);
}
});

}
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}

Answer

Replace Your Snackbar code

Snackbar snackbar = Snackbar
                        .make(view, name + " Removed!", Snackbar.LENGTH_LONG)
                        .setActionTextColor(context.getResources().getColor(R.color.dot_dark_screen1))
                        .setAction("UNDO", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                context = view.getContext();
                                final User mDataSet3 = mDataSet.get(getAdapterPosition());;
                                String url = mDataSet3.getEmail();
                                String name = mDataSet3.getName();
                                String query = "INSERT INTO faces (name,url) VALUES('"+name+"', '"+url+"');";
                                db.execSQL(query);
                                mDataSet.add(mDataSet3);
                                 notifyDataSetChanged();
                                Snackbar snackbar3 = Snackbar.make(view, name + " Favorited!", Snackbar.LENGTH_SHORT);
                                snackbar3.show();
                            }
                        });
                snackbar.show();