简体   繁体   English

将新的接收/发送消息添加到列表视图Android

[英]Add new received/send message to listview Android

I'm making an android app to send and receive text messages. 我正在制作一个Android应用程序来发送和接收短信。 But I can't show new incoming messages in my ListView. 但是我无法在ListView中显示新的传入消息。

Here is the code that I use to populate the ListView: 这是我用来填充ListView的代码:

ListView lv = (ListView) view.findViewById(R.id.lvConversations);
Cursor c = getActivity().getContentResolver().query(INBOX_URI, projection, "address = '" + address + "'", null, "date");
ConversationCursorAdapter adap = new ConversationCursorAdapter(getActivity(),c);
lv.setAdapter(adap);
lv.setSelection(lv.getCount() - 1);

And here is my ConversationCursorAdapter 这是我的ConversationCursorAdapter

public class ConversationCursorAdapter extends CursorAdapter {

public ConversationCursorAdapter(Context context, Cursor c) {
    super(context, c, 0);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.msg, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView tvBody = (TextView) view.findViewById(R.id.tvBody);
    TextView tvDate = (TextView) view.findViewById(R.id.tvDate);
    String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
    Long date = cursor.getLong(cursor.getColumnIndexOrThrow("date"));
    tvBody.setText(body);
    tvDate.setText(date.toString());

}}

I also have a listener to get the new messages, but let's first concentrate on the sending messages. 我也有一个侦听器来获取新消息,但让我们首先集中精力发送消息。 When the user clicks on the sendbutton the message is send, but doesn't show in the listview. 当用户单击send按钮时,消息已发送,但不在列表视图中显示。 Here is the code to send the message. 这是发送消息的代码。

Button btnSend = (Button) view.findViewById(R.id.btnSend);
    btnSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText et = (EditText) getView().findViewById(R.id.etSendMessage);
            SmsManager manager = SmsManager.getDefault() ;
            manager.sendTextMessage(toAddress, null, body, null, null);
            et.setText("");
        }
    });

Could somebody tell me what I'm doing wrong? 有人可以告诉我我在做什么错吗? Thank you! 谢谢!

First create broadcast receiver with some unique action like String ACTION = "message.received"; 首先用一些独特的动作创建广播接收器,例如String ACTION =“ message.received”;

use this action string as intent filter action string 使用此操作字符串作为意图过滤器操作字符串

then notifiy that broadcast receiver when you are getting new data while API. 然后在通过API获取新数据时通知该广播接收器。

then add new row to list view then you can archive by notifyDataSetChanged(); 然后将新行添加到列表视图,然后可以通过notifyDataSetChanged()进行归档;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM