Android 未加载到ListView的Sms消息
我正在做一个android项目,我一直在把短信加载到应用程序中。这就是我遇到的错误: 我正在尝试设置列表视图,并在这段代码中使用阵列适配器: 短信收件箱Android 未加载到ListView的Sms消息,android,android-listview,smsmanager,Android,Android Listview,Smsmanager,我正在做一个android项目,我一直在把短信加载到应用程序中。这就是我遇到的错误: 我正在尝试设置列表视图,并在这段代码中使用阵列适配器: 短信收件箱 private fun fetchInbox(): ArrayList<String> { val sms: ArrayList<String> = ArrayList() val uri: Uri = Uri.parse("contact://sms/inbox") val curso
private fun fetchInbox(): ArrayList<String> {
val sms: ArrayList<String> = ArrayList()
val uri: Uri = Uri.parse("contact://sms/inbox")
val cursor: Cursor = contentResolver.query(uri, arrayOf("id", "address", "date", "body"), null, null, null)
while (cursor.moveToNext()){
val address: String = cursor.getString(1)
val body: String = cursor.getString(3)
sms.add("Address: $address" + "Message: $body")
}
return sms
}
private fun fetchInbox():ArrayList{
val sms:ArrayList=ArrayList()
val uri:uri=uri.parse(“contact://sms/inbox")
val cursor:cursor=contentResolver.query(uri,数组(“id”,“地址”,“日期”,“正文”),null,null,null)
while(cursor.moveToNext()){
val地址:String=cursor.getString(1)
val body:String=cursor.getString(3)
添加(“地址:$Address”+“消息:$body”)
}
回复短信
}
然后我在我的主要活动中这样称呼它:
val listView = text_Messages
if(fetchInbox() != null) {
val adapter: ArrayAdapter<String> = ArrayAdapter(this, android.R.layout.activity_list_item, fetchInbox())
listView.adapter = adapter
}
val listView=text\u消息
如果(fetchInbox()!=null){
val适配器:ArrayAdapter=ArrayAdapter(此,android.R.layout.activity\u列表\项,fetchInbox())
listView.adapter=适配器
}
我只是不明白为什么会发生这种错误
ListView lViewSMS = (ListView) findViewById(R.id.listViewSMS);
if(fetchInbox()!=null)
{
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, fetchInbox());
lViewSMS.setAdapter(adapter);
}
//put the above part in oncreateview()
public ArrayList fetchInbox()
{
ArrayList sms = new ArrayList();
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor cursor = getContentResolver().query(uriSms, new String[]{"_id", "address", "date", "body","" + "person"},null,null,null);
cursor.moveToFirst();
while (cursor.moveToNext())
{
String match="is created";
String address = cursor.getString(1);
String body = cursor.getString(3);
// String title = cursor.getString(3);
String tdata=body;
sms.add(""+""+body);
}
return sms;
}