gpt4 book ai didi

android - 如何在聊天应用程序中存储上次查看的消息

转载 作者:行者123 更新时间:2023-11-30 00:31:05 28 4
gpt4 key购买 nike

我尝试使用以下代码实现群聊。

    public class mainchat extends AppCompatActivity {

private ImageButton send;
private FirebaseAuth mAuth;
FirebaseDatabase database;
DatabaseReference myRef, myref3;
HashMap<String, String> msg;
EditText sending;
ListView lchat;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainchat);

//all permissions
mAuth = FirebaseAuth.getInstance();
database = FirebaseDatabase.getInstance();
myRef = database.getReference();
myref3 = database.getReference("chat");
myref3.keepSynced(true);

//storing
final ArrayList<HashMap<String, String>> chatmsgs = new ArrayList<>();

msg = new HashMap<>();
SharedPreferences prefs = getSharedPreferences("profiledata", Context.MODE_PRIVATE);
final String usernameee = prefs.getString("username", "username failed");

//adapters
final chatadapter ch = new chatadapter(this, chatmsgs,usernameee);
lchat = (ListView) findViewById(R.id.chatlist);
lchat.setAdapter(ch);

//defination
send = (ImageButton) findViewById(R.id.sendbutton1);
sending = (EditText) findViewById(R.id.sendingtext);


send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isOnline()){
if (sending.getText().toString().equals("")) {

} else {
msg.put("message", sending.getText().toString());
sending.setText("");
msg.put("username", usernameee);
msg.put("time", new SimpleDateFormat("HH:mm").format(Calendar.getInstance().getTime()));
myRef.child("chat").push().setValue(msg);
}
}
else{
Toast.makeText(mainchat.this,"please connect to internet",Toast.LENGTH_LONG).show();
}
}
});

myref3.limitToLast(15).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
HashMap<String, String> k = (HashMap<String, String>) dataSnapshot.getValue();
chatmsgs.add(k);
ch.notifyDataSetChanged();
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

}
protected boolean isOnline()
{
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) { // connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
if (activeNetwork.isConnected())
haveConnectedWifi = true;
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
if (activeNetwork.isConnected())
haveConnectedMobile = true;
}
}

return haveConnectedWifi || haveConnectedMobile;
}
}

and the chat adapter class used is:

    public class chatadapter extends ArrayAdapter<HashMap<String,String>> {

private String name;
chatadapter(Activity context, ArrayList<HashMap<String,String>> a,String name){
super(context,0,a);
this.name=name;
}


@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View l=convertView;
HashMap<String,String> msgmsg=getItem(position);
if(true){
if(msgmsg.get("username").equals(name)){
l = LayoutInflater.from(getContext()).inflate(R.layout.activity_rightpanel, parent, false);
}
else{
l = LayoutInflater.from(getContext()).inflate(R.layout.activity_leftpanel, parent, false);
}
}

TextView x=(TextView) l.findViewById(R.id.username);
TextView y=(TextView) l.findViewById(msg);
TextView z=(TextView) l.findViewById(R.id.timing);

x.setText(msgmsg.get("username"));
y.setText(msgmsg.get("message"));
z.setText(msgmsg.get("time"));

return l;

}
}

现在我想向所有用户显示未读消息计数的通知。并且还希望通过一行(在上面我们看到消息)说一些未读消息和下面的未读消息来表达聊天 Activity 的 Intent 。我怎样才能做到这一点?请帮我解决这个问题......

最佳答案

相反,您可以使用共享首选项来存储上次查看的消息 (id),这样如果新收到的消息数量超过它,您可以说用小横幅或其他东西区分未查看的消息。希望这对你有帮助。

关于android - 如何在聊天应用程序中存储上次查看的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44341532/

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