在listview上阅读新消息(read new message top on listview)

编程入门 行业动态 更新时间:2024-10-26 06:28:01
在listview上阅读新消息(read new message top on listview)

我正在尝试一个聊天应用程序。 用户可以写入和读取,但是当用户写入消息时,它将转到列表的底部..我想要反转。 我想将新消息列入清单。

我试了2天。

public class MainActivity extends AppCompatActivity { private Firebase mFirebaseRef; FirebaseListAdapter<ChatMessage> mListAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Firebase.setAndroidContext(this); setContentView(R.layout.activity_main); mFirebaseRef = new Firebase("https://hkhkhh.firebaseio.com"); final EditText textEdit = (EditText) this.findViewById(R.id.text_edit); Button sendButton = (Button) this.findViewById(R.id.send_button); sendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String text = textEdit.getText().toString(); Map<String,Object> values = new HashMap<>(); values.put("name", "Android User"); values.put("text", text); mFirebaseRef.push().setValue(values); textEdit.setText(""); } }); final ListView listView = (ListView) this.findViewById(android.R.id.list); mListAdapter = new FirebaseListAdapter<ChatMessage>(this, ChatMessage.class, android.R.layout.two_line_list_item, mFirebaseRef) { @Override protected void populateView(View v, ChatMessage model, int position) { ((TextView)v.findViewById(android.R.id.text1)).setText(model.getName()); ((TextView)v.findViewById(android.R.id.text2)).setText(model.getText()); } }; listView.setAdapter(mListAdapter); } @Override protected void onDestroy() { super.onDestroy(); mListAdapter.cleanup(); } }

和chatmessage java

package com.ankayapim.ozan.kagit23; public class ChatMessage { private String name; private String text; public ChatMessage() { // necessary for Firebase's deserializer } public ChatMessage(String name, String text) { this.name = name; this.text = text; } public String getName() { return name; } public String getText() { return text; } }

I'm trying crate a chat app. Users can write and read but when a user writes message it's going to the bottom of the list.. I want to reverse. I want to new message go top on list.

I try several ways 2 days.

public class MainActivity extends AppCompatActivity { private Firebase mFirebaseRef; FirebaseListAdapter<ChatMessage> mListAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Firebase.setAndroidContext(this); setContentView(R.layout.activity_main); mFirebaseRef = new Firebase("https://hkhkhh.firebaseio.com"); final EditText textEdit = (EditText) this.findViewById(R.id.text_edit); Button sendButton = (Button) this.findViewById(R.id.send_button); sendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String text = textEdit.getText().toString(); Map<String,Object> values = new HashMap<>(); values.put("name", "Android User"); values.put("text", text); mFirebaseRef.push().setValue(values); textEdit.setText(""); } }); final ListView listView = (ListView) this.findViewById(android.R.id.list); mListAdapter = new FirebaseListAdapter<ChatMessage>(this, ChatMessage.class, android.R.layout.two_line_list_item, mFirebaseRef) { @Override protected void populateView(View v, ChatMessage model, int position) { ((TextView)v.findViewById(android.R.id.text1)).setText(model.getName()); ((TextView)v.findViewById(android.R.id.text2)).setText(model.getText()); } }; listView.setAdapter(mListAdapter); } @Override protected void onDestroy() { super.onDestroy(); mListAdapter.cleanup(); } }

and chatmessage java

package com.ankayapim.ozan.kagit23; public class ChatMessage { private String name; private String text; public ChatMessage() { // necessary for Firebase's deserializer } public ChatMessage(String name, String text) { this.name = name; this.text = text; } public String getName() { return name; } public String getText() { return text; } }

最满意答案

假设您的ChatMessage作为arraylist和mFirebaseRef的对象作为适配器列表。 你应该给arraylist添加新的消息。

ChatMessage newmsg = new ChatMessage(); //give text to newmsg mFirebaseRef.add(0,newmsg); mListAdapter .addAll(mFirebaseRef);

在适配器类上创建一个公共方法addAll。

public void addAll(ArrayList<ChatMessage> data) { try { if (data != null){ this.mData.clear(); this.mData.addAll(data); } else this.mData = new ArrayList<>() } catch (Exception e) { e.printStackTrace(); } notifyDataSetChanged(); }

调用notifyDataSetChanged();

assuming your ChatMessage as your objects for arraylist and mFirebaseRef as your adapter list. you should add new message to arraylist.

ChatMessage newmsg = new ChatMessage(); //give text to newmsg mFirebaseRef.add(0,newmsg); mListAdapter .addAll(mFirebaseRef);

create a public method addAll on your adapter class.

public void addAll(ArrayList<ChatMessage> data) { try { if (data != null){ this.mData.clear(); this.mData.addAll(data); } else this.mData = new ArrayList<>() } catch (Exception e) { e.printStackTrace(); } notifyDataSetChanged(); }

call notifyDataSetChanged();

更多推荐

本文发布于:2023-07-29 14:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1316755.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:新消息   listview   read   top   message

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!