gpt4 book ai didi

java - dataSnapshot 检索最后一条消息但难以在小部件上显示它......?

转载 作者:太空狗 更新时间:2023-10-29 13:47:46 24 4
gpt4 key购买 nike

完成一个聊天应用程序并在这里研究了很多问题(发现 1 个让我步入正轨 setTextViewText not updating widget)正如您将从我的代码中看到的那样,我已经关注了那个问题,但是,Widget 没有更新消息。我已调试并可以确认已检索到最后一条消息。

我的代码:

public class ProTalkWidget extends AppWidgetProvider {

private static final String ACTION_BROADCASTWIIDGET = "ACTION_BROADCASTWIIDGET";
public static final String PROTALK_APPWIDGET_UPDATE = "PROTALK_APPWIDGET_UPDATE";

private final int REQUEST_CODE = 0;
public static final String USER_ID = "userId";
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
FirebaseAuth mAuth = FirebaseAuth.getInstance();
String msgReceiverId;

private void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
final int appWidgetId) {

//Construct the RemoteViews object
final RemoteViews views;
views = new RemoteViews(context.getPackageName(), R.layout.pro_talk_widget);




Intent mainIntent = new Intent(context, ChatRoomActivity.class);
PendingIntent mainPendingIntent = PendingIntent.getActivity(context, REQUEST_CODE, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);


views.setOnClickPendingIntent(R.id.widget_layout, mainPendingIntent);
Intent intent = new Intent(context, ProTalkWidget.class);
intent.setAction(ACTION_BROADCASTWIIDGET);

context.sendBroadcast(intent);

appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.updateAppWidget(appWidgetId, views);
}

@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);



if(PROTALK_APPWIDGET_UPDATE.equals(intent.getAction())){

Log.i("onReceive", "getting onReceived!");


handleMsg(intent, context);


}
}


private void handleMsg(Intent intent, final Context ctx){

msgReceiverId = intent.getStringExtra("receiverId");


if(msgReceiverId!=null) {
final RemoteViews views;
views = new RemoteViews(ctx.getPackageName(), R.layout.pro_talk_widget);

String my_Id = mAuth.getCurrentUser().getUid();


DatabaseReference msgRef = rootRef.child("Messages").child(my_Id).child(msgReceiverId);
Query msgQuery = msgRef.limitToLast(1);

msgQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

if(views != null) {

Messages messages = dataSnapshot.getValue(Messages.class);
String msg = messages.getMessage();

Log.i("Views", "views");

views.setTextViewText(R.id.appwidget_message, msg);

}else{
Log.i("Views", "not working");
}
}


@Override
public void onCancelled(DatabaseError databaseError) {

}
});

}

}


@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}

@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}

@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
}

非常感谢和欢迎任何帮助或资源或建议。

谢谢

Appwidget with no message being displayed

加上日志,我没有收到任何错误,只是 View 日志,我猜它代表了所有加载的消息(不确定我是新手!)。

再次感谢!

消息

public class Messages {

private String message;
private String from;
private String type;
private long time;
private boolean seen;

public Messages(){}

public Messages(String message,String from, String type, long time, boolean seen) {
this.message = message;
this.from = from;
this.type = type;
this.time = time;
this.seen = seen;
}



public String getMessage() {
return message;

}

public void setMessage(String message) {
this.message = message;
}

public String getFrom() {
return from;
}

public void setFrom(String from) {
this.from = from;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public long getTime() {
return time;
}

public void setTime(long time) {
this.time = time;
}

public boolean isSeen() {
return seen;
}

public void setSeen(boolean seen) {
this.seen = seen;
}
}

日志 logcat

消息的调试器数据快照值 datasnapshot

Firebase 数据库结构 DB Structure

最佳答案

根据您的意见和查看您的数据库结构,我可以告诉您,您需要循环遍历 msgReceiverId 节点以使用 getChildren() 方法获取其子节点,例如这个:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("Messages").child(my_Id).child(msgReceiverId).limitToLast(1);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Messages messages = dataSnapshot.getValue(Messages.class);
String message = messages.getMessage();
Log.d("TAG", message.getMessage());
}
}

@Override
public void onCancelled(DatabaseError databaseError) {}
};
query.addListenerForSingleValueEvent(valueEventListener);

logcat 中的输出将是最后一条短信。

关于java - dataSnapshot 检索最后一条消息但难以在小部件上显示它......?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51584723/

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