SMS 消息未加载到 ListView
Sms Messages not Loading into ListView
我正在做一个 android 项目,但我无法将 SMS 消息加载到应用程序中。这是我遇到的错误:
我正在尝试在这段代码中设置列表视图并使用数组适配器:
短信收件箱
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
}
然后我在我的 MainActivity 中这样调用它:
val listView = text_Messages
if(fetchInbox() != null) {
val adapter: ArrayAdapter<String> = ArrayAdapter(this, android.R.layout.activity_list_item, fetchInbox())
listView.adapter = 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;
}
我正在做一个 android 项目,但我无法将 SMS 消息加载到应用程序中。这是我遇到的错误:
我正在尝试在这段代码中设置列表视图并使用数组适配器:
短信收件箱
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
}
然后我在我的 MainActivity 中这样调用它:
val listView = text_Messages
if(fetchInbox() != null) {
val adapter: ArrayAdapter<String> = ArrayAdapter(this, android.R.layout.activity_list_item, fetchInbox())
listView.adapter = 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;
}