在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();
更多推荐
发布评论