【问题标题】:read new message top on listview在列表视图上阅读新消息顶部
【发布时间】:2016-02-24 14:56:52
【问题描述】:

我正在尝试创建一个聊天应用程序。 用户可以写和读,但是当用户写消息时,它会排在列表的底部。我想反转。我想把新消息放在列表的首位。

我在 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();
    }
}

和 聊天消息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;
    }
}

【问题讨论】:

    标签: android list view


    【解决方案1】:

    假设您的 ChatMessage 作为您的数组列表对象和 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();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多