gpt4 book ai didi

java - Android notificationDataSetChanged 未更新现有项目的 View ?

转载 作者:行者123 更新时间:2023-12-01 09:47:17 25 4
gpt4 key购买 nike

Android 新手,很难弄清楚为什么 notifyDataSetChanged(); 在重新插入时不更新从 View 中删除的项目。删除项目时效果很好,但在将项目恢复到 View 中的现有项目时效果不佳。如有任何帮助,我们将不胜感激。

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("xxx", 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("xxx 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("xxx 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, "xxx", 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);
}
}

最佳答案

替换您的 Snackbar 代码

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

关于java - Android notificationDataSetChanged 未更新现有项目的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37893514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com