聊天应用程序的完成并在这里研究了很多问题(发现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
在日志中没有出现任何错误,我认为视图的日志代表了所有已加载的消息(确保我是新手!)。
再次感谢!
消息
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的调试器datasnapshot值
Firebase数据库结构DB Structure
答案 0 :(得分:1)
根据您的评论并查看您的数据库结构,我可以告诉您,您需要像这样循环使用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中的输出将是最后一条短信。