gpt4 book ai didi

How to get firebase notication dbhelper to adapter set?(如何获取Firebase通知的DBhelper到适配器集?)

转载 作者:bug小助手 更新时间:2023-10-24 18:54:14 26 4
gpt4 key购买 nike



This firebase notification createNotification(unique_id, title, message, big_image, link, post_id); data save dbhelper. adapter data not show.How to get dbhelper to adapter data pass

此FireBase通知创建了Notify(唯一id、标题、消息、Big_Image、link、post_id);数据保存数据库帮助器。适配器数据未显示。如何让DBHelper将适配器数据传递


  public class MyFirebaseMessageService extends FirebaseMessagingService {

@Override
public void onNewToken(@NonNull String token) {
super.onNewToken(token);
}

@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);

if (remoteMessage.getData().size() > 0) {
Map<String, String> data = remoteMessage.getData();
Log.d("onMessageFirebase: ", remoteMessage.getData().toString());

if (data.get("post_id") != null) {
String _unique_id = data.get("unique_id");
String title = data.get("title");
String message = data.get("message");
String big_image = data.get("big_image");
String image = data.get("image");
String content_type = data.get("content_type");
String video_id = data.get("video_id");


String link = data.get("link");
String _post_id = data.get("post_id");

assert _unique_id != null;
long unique_id = Long.parseLong(_unique_id);

assert _post_id != null;
long post_id = Long.parseLong(_post_id);

DbHandler db = new DbHandler(this);
db.notifcation(unique_id, title, message, big_image,image,content_type,video_id, link, post_id);

createNotification(unique_id, title, message, big_image, link, post_id);
}
}
}

private void createNotification(long unique_id, String title, String message, String image_url, String link, long post_id) {

Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("unique_id", unique_id);
intent.putExtra("post_id", post_id);
intent.putExtra("title", title);
intent.putExtra("link", link);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = getApplicationContext().getString(R.string.app_name);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(getNotificationIcon(notificationBuilder))
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_notification_large_icon))
.setContentTitle(title)
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentIntent(pendingIntent);

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
notificationBuilder.setPriority(Notification.PRIORITY_MAX);
} else {
notificationBuilder.setPriority(NotificationManager.IMPORTANCE_HIGH);
}

Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder.setSound(alarmSound).setVibrate(new long[]{100, 200, 300, 400});

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.shouldShowLights();
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(false);
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel);
}

if (image_url != null && !image_url.isEmpty()) {
Bitmap image = fetchBitmap(image_url);
if (image != null) {
notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image));
}
}

//assert notificationManager != null;
notificationManager.notify((int) post_id, notificationBuilder.build());
}

private int getNotificationIcon(NotificationCompat.Builder notificationBuilder) {
notificationBuilder.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
return R.drawable.ic_notification_large_icon;
}

private Bitmap fetchBitmap(String src) {
try {
if (src != null) {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setConnectTimeout(1200000);
connection.setReadTimeout(1200000);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
}
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
}

dbhelper save data public void notifcation(long unique_id, String title, String message, String big_image,String image,String content_type,String video_id, String link, long post_id) {

DBhelper SAVE DATA PUBLIC VID NOTIFICATION(LONG UNIQUE_ID,STRING标题,STRING MESSAGE,STRING BIG_IMAGE,STRING IMAGE,STRING CONTENT_TYPE,STRING VIDEO_ID,STRING LINK,LONG POST_ID){


    public class DbHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 6;
private static final String DATABASE_NAME = "db_recipes_favorite";
private static final String TABLE_NAME = "tbl_recipes_favorite";

private static final String TABLE_NOTIFICATION = "notification1";
private static final String KEY_ID = "id";
private static final String KEY_CAT_NAME = "category_name";
private static final String KEY_RECIPE_ID = "recipe_id";
private static final String KEY_RECIPE_TITLE = "recipes_title";
private static final String KEY_RECIPE_TIME = "recipe_time";
private static final String KEY_RECIPE_IMAGE = "recipe_image";
private static final String KEY_RECIPE_DESCRIPTION = "recipe_description";
private static final String KEY_VIDEO_URL = "video_url";
private static final String KEY_VIDEO_ID = "video_id";
private static final String KEY_CONTENT_TYPE = "content_type";
private static final String KEY_FEATURED = "featured";
private static final String KEY_TAGS = "tags";
private static final String KEY_TOTAL_VIEWS = "total_views";

private static final String unique_id = "unique_id";
private static final String id = "id";
private static final String title = "title";
private static final String message = "message";
private static final String big_image = "big_image";

private static final String image = "image";

private static final String content_type = "content_type";

private static final String video_id = "video_id";

private static final String link = "link";
private static final String post_id = "post_id";
public DbHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_CAT_NAME + " TEXT,"
+ KEY_RECIPE_ID + " TEXT,"
+ KEY_RECIPE_TITLE + " TEXT,"
+ KEY_RECIPE_TIME + " TEXT,"
+ KEY_RECIPE_IMAGE + " TEXT,"
+ KEY_RECIPE_DESCRIPTION + " TEXT,"
+ KEY_VIDEO_URL + " TEXT,"
+ KEY_VIDEO_ID + " TEXT,"
+ KEY_CONTENT_TYPE + " TEXT,"
+ KEY_FEATURED + " TEXT,"
+ KEY_TAGS + " TEXT,"
+ KEY_TOTAL_VIEWS + " TEXT"
+ ")";

db.execSQL(CREATE_CONTACTS_TABLE);

String CREATE_CONTACTS_NOTIFICATION = "CREATE TABLE " + TABLE_NOTIFICATION + "("
+ id + " INTEGER PRIMARY KEY,"
+ unique_id + " TEXT,"
+ title + " TEXT,"
+ message + " TEXT,"
+ big_image + " TEXT,"
+ image + " TEXT,"
+ content_type + " TEXT,"
+ video_id + " TEXT,"
+ link + " TEXT,"
+ post_id + " TEXT"
+ ")";


db.execSQL(CREATE_CONTACTS_NOTIFICATION);

}



// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

// Create tables again
onCreate(db);


db.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTIFICATION);

// Create tables again
onCreate(db);
}

//Adding Record in Database

public void AddtoFavorite(Recipe p) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_CAT_NAME, p.getCategory_name());
values.put(KEY_RECIPE_ID, p.getRecipe_id());
values.put(KEY_RECIPE_TITLE, p.getRecipe_title());
values.put(KEY_RECIPE_TIME, p.getRecipe_time());
values.put(KEY_RECIPE_IMAGE, p.getRecipe_image());
values.put(KEY_RECIPE_DESCRIPTION, p.getRecipe_description());
values.put(KEY_VIDEO_URL, p.getVideo_url());
values.put(KEY_VIDEO_ID, p.getVideo_id());
values.put(KEY_CONTENT_TYPE, p.getContent_type());
values.put(KEY_FEATURED, p.getFeatured());
values.put(KEY_TAGS, p.getTags());
values.put(KEY_TOTAL_VIEWS, p.getTotal_views());

// Inserting Row
db.insert(TABLE_NAME, null, values);
db.close(); // Closing database connection

}

// Getting All Data
public List<Recipe> getAllData() {
List<Recipe> dataList = new ArrayList<Recipe>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_NAME + " ORDER BY id DESC";

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Recipe values = new Recipe();
values.setId(Integer.parseInt(cursor.getString(0)));
values.setCategory_name(cursor.getString(1));
values.setRecipe_id(cursor.getString(2));
values.setRecipe_title(cursor.getString(3));
values.setRecipe_time(cursor.getString(4));
values.setRecipe_image(cursor.getString(5));
values.setRecipe_description(cursor.getString(6));
values.setVideo_url(cursor.getString(7));
values.setVideo_id(cursor.getString(8));
values.setContent_type(cursor.getString(9));
values.setFeatured(cursor.getString(10));
values.setTags(cursor.getString(11));
values.setTotal_views(cursor.getLong(12));
// Adding contact to list
dataList.add(values);
} while (cursor.moveToNext());
}

// return contact list
return dataList;
}






public List<bell> getAllbell() {
List<bell> dataList = new ArrayList<bell>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_NOTIFICATION + " ORDER BY id DESC";

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
bell values = new bell();
values.setId(Integer.parseInt(cursor.getString(0)));
values.setunique_id(Integer.parseInt(cursor.getString(1)));
values.settitle(cursor.getString(1));
values.setmessage(cursor.getString(2));
values.setbig_image(cursor.getString(3));
values.setimage(cursor.getString(4));
values.setcontent_type(cursor.getString(5));
values.setpost_id(cursor.getString(6));
dataList.add(values);
} while (cursor.moveToNext());
}

// return contact list
return dataList;
}










//getting single row
public List<Recipe> getFavRow(String id) {
List<Recipe> dataList = new ArrayList<Recipe>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_NAME + " WHERE recipe_id=" + id;

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Recipe values = new Recipe();
values.setId(Integer.parseInt(cursor.getString(0)));
values.setCategory_name(cursor.getString(1));
values.setRecipe_id(cursor.getString(2));
values.setRecipe_title(cursor.getString(3));
values.setRecipe_time(cursor.getString(4));
values.setRecipe_image(cursor.getString(5));
values.setRecipe_description(cursor.getString(6));
values.setVideo_url(cursor.getString(7));
values.setVideo_id(cursor.getString(8));
values.setContent_type(cursor.getString(9));
values.setFeatured(cursor.getString(10));
values.setTags(cursor.getString(11));
values.setTotal_views(cursor.getLong(12));
// Adding contact to list
dataList.add(values);
} while (cursor.moveToNext());
}

// return contact list
return dataList;
}

//for remove favorite
public void RemoveFav(Recipe contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, KEY_RECIPE_ID + " = ?",
new String[]{String.valueOf(contact.getRecipe_id())});
db.close();
}
public void notifcation(long unique_id, String title, String message, String big_image,String
image,String content_type,String video_id, String link, long post_id) {

SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put("unique_id", unique_id);
values.put("title", title);
values.put("message", message);
values.put("big_image", big_image);
values.put("image", image);
values.put("content_type", content_type);
values.put("video_id", video_id);
values.put("link", link);
values.put("post_id", post_id);


// Inserting Row
db.insert(TABLE_NOTIFICATION, null, values);
db.close(); // Closing database connection




}

public enum DatabaseManager {
INSTANCE;
private SQLiteDatabase db;
private boolean isDbClosed = true;
DbHandler dbHelper;

public void init(Context context) {
dbHelper = new DbHandler(context);
if (isDbClosed) {
isDbClosed = false;
this.db = dbHelper.getWritableDatabase();
}

}

public boolean isDatabaseClosed() {
return isDbClosed;
}

public void closeDatabase() {
if (!isDbClosed && db != null) {
isDbClosed = true;
db.close();
dbHelper.close();
}
}
}

}

adapter

转接器


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

private final int VIEW_ITEM = 1;
private final int VIEW_PROG = 0;

private List<bell> items;

private boolean loading;
private OnLoadMoreListener onLoadMoreListener;

private Context context;
private OnItemClickListener mOnItemClickListener;
boolean scrolling = false;

private SharedPref sharedPref;




public interface OnItemClickListener {
void onItemClick(View view, bell obj, int position);
}

public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mOnItemClickListener = mItemClickListener;
}

public Adapterbell(Context context, List<bell> items) {
this.items = items;
this.context = context;
this.sharedPref = new SharedPref(context);


}

public class OriginalViewHolder extends RecyclerView.ViewHolder {

public TextView category_name;
public TextView recipe_title;
public ImageView recipe_image;
public ImageView thumbnail_video;
public MaterialRippleLayout lyt_parent;

public OriginalViewHolder(View v) {
super(v);
category_name = v.findViewById(R.id.category_name);
recipe_title = v.findViewById(R.id.recipe_title);
recipe_image = v.findViewById(R.id.recipe_image);
thumbnail_video = v.findViewById(R.id.thumbnail_video);
lyt_parent = v.findViewById(R.id.lyt_parent);
}

}

public static class ProgressViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;

public ProgressViewHolder(View v) {
super(v);
progressBar = v.findViewById(R.id.load_more);
}
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
RecyclerView.ViewHolder vh;
if (viewType == VIEW_ITEM) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_recipe, parent, false);
vh = new OriginalViewHolder(v);
} else {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_load_more, parent, false);
vh = new ProgressViewHolder(v);
}
return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof OriginalViewHolder) {
final bell b = items.get(position);
final OriginalViewHolder vItem = (OriginalViewHolder) holder;


vItem.recipe_title.setText(b.title);

if (b.content_type != null && b.content_type.equals("youtube")) {
Picasso.get()
.load(Constant.YOUTUBE_IMAGE_FRONT + b.video_id + Constant.YOUTUBE_IMAGE_BACK_MQ)
.placeholder(R.drawable.ic_thumbnail)
.into(vItem.recipe_image);
} else {
Picasso.get()
.load(sharedPref.getApiUrl() + "/upload/" + b.image)
.placeholder(R.drawable.ic_thumbnail)
.into(vItem.recipe_image);
}

if (b.content_type != null && b.content_type.equals("Post")) {
vItem.thumbnail_video.setVisibility(View.GONE);
} else {
vItem.thumbnail_video.setVisibility(View.VISIBLE);
}

vItem.lyt_parent.setOnClickListener(view -> {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(view, b, position);
}
});

} else {
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
}

if (getItemViewType(position) == VIEW_PROG) {
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
layoutParams.setFullSpan(true);
} else {
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
layoutParams.setFullSpan(false);
}

}

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

@Override
public int getItemViewType(int position) {
if (items.get(position) != null) {
return VIEW_ITEM;
} else {
return VIEW_PROG;
}
}

public void insertData(List<bell> items) {
setLoaded();
int positionStart = getItemCount();
int itemCount = items.size();
this.items.addAll(items);
notifyItemRangeInserted(positionStart, itemCount);
}

public void setLoaded() {
loading = false;
for (int i = 0; i < getItemCount(); i++) {
if (items.get(i) == null) {
items.remove(i);
notifyItemRemoved(i);
}
}
}

public void setLoading() {
if (getItemCount() != 0) {
this.items.add(null);
notifyItemInserted(getItemCount() - 1);
loading = true;
}
}

public void resetListData() {
this.items = new ArrayList<>();
notifyDataSetChanged();
}

public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener = onLoadMoreListener;
}

private void lastItemViewDetector(RecyclerView recyclerView) {

if (recyclerView.getLayoutManager() instanceof StaggeredGridLayoutManager) {
final StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) recyclerView.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int lastPos = getLastVisibleItem(layoutManager.findLastVisibleItemPositions(null));
if (!loading && lastPos == getItemCount() - 1 && onLoadMoreListener != null) {
int current_page = getItemCount() / AppConfig.POST_PER_PAGE;
onLoadMoreListener.onLoadMore(current_page);
loading = true;
}
}
});
}
}

public interface OnLoadMoreListener {
void onLoadMore(int current_page);
}

private int getLastVisibleItem(int[] into) {
int last_idx = into[0];
for (int i : into) {
if (last_idx < i) last_idx = i;
}
return last_idx;
}

}

更多回答
优秀答案推荐
更多回答

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