我正在制作一个android应用程序来发送和接收短信。
但我无法在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);
这是我的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());
}}
我也有一个侦听器来接收新消息,但让我们首先集中精力发送消息。当用户单击发送按钮时,消息被发送,但不会显示在列表视图中。这是发送消息的代码。
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("");
}
});
有人能告诉我我做错了什么吗?
非常感谢。