如何在 `ListView` 中显示来自特定 phone 号码的消息?
How to display messages from a particular phone number in `ListView`?
我必须在 ListView
中显示来自给定号码的 SMS
条消息。
我已经显示了来自我在程序中描述的号码的消息。
现在我必须使用 EditText
获取号码并显示来自该号码的消息。
我的 MainActivity.java
是
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView) findViewById(R.id.listView);
List<String> msgList=getSMS();
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,msgList);
listView.setAdapter(adapter);
}
public List<String> getSMS(){
List<String>sms=new ArrayList<String>();
final String SMS_URI_INBOX="content://sms/inbox";
try {
Uri uri=Uri.parse(SMS_URI_INBOX);
String[] projection=new String[]{"_id","address","person","body","date","type"};
Cursor cursor=getContentResolver().query(uri,projection,"address='+918870346164'",null,null);
if (cursor.moveToFirst()){
int index_Address = cursor.getColumnIndex("address");
int index_Person = cursor.getColumnIndex("person");
int index_Body = cursor.getColumnIndex("body");
int index_Date = cursor.getColumnIndex("date");
int index_Type = cursor.getColumnIndex("type");
do {
String strAddress = cursor.getString(index_Address);
int intPerson = cursor.getInt(index_Person);
String strBody = cursor.getString(index_Body);
String longDate = cursor.getString(index_Date);
int intType = cursor.getInt(index_Type);
sms.add("\n"+strBody+"\n");
}while (cursor.moveToNext());
if (!cursor.isClosed()){
cursor.close();
cursor=null;
}
}
}catch (SQLiteException e){
Log.d("SQLiteException",e.getMessage());
}
return sms;
}
在这一行中我定义了我想使用的号码
Cursor cursor=getContentResolver().query(uri,projection,"address='+918870346164'",null,null);
在这里,我必须通过 TextView
从用户那里获取号码。
请帮忙
我认为你需要了解 Android Training
但无论如何EditText getText()
EditText et = (EditText) findViewById(R.id.edit_text_id);
String address="address='"+et.getText().toString()+"'";
cursor=getContentResolver().query(uri,projection,address,null,null);
我必须在 ListView
中显示来自给定号码的 SMS
条消息。
我已经显示了来自我在程序中描述的号码的消息。
现在我必须使用 EditText
获取号码并显示来自该号码的消息。
我的 MainActivity.java
是
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView) findViewById(R.id.listView);
List<String> msgList=getSMS();
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,msgList);
listView.setAdapter(adapter);
}
public List<String> getSMS(){
List<String>sms=new ArrayList<String>();
final String SMS_URI_INBOX="content://sms/inbox";
try {
Uri uri=Uri.parse(SMS_URI_INBOX);
String[] projection=new String[]{"_id","address","person","body","date","type"};
Cursor cursor=getContentResolver().query(uri,projection,"address='+918870346164'",null,null);
if (cursor.moveToFirst()){
int index_Address = cursor.getColumnIndex("address");
int index_Person = cursor.getColumnIndex("person");
int index_Body = cursor.getColumnIndex("body");
int index_Date = cursor.getColumnIndex("date");
int index_Type = cursor.getColumnIndex("type");
do {
String strAddress = cursor.getString(index_Address);
int intPerson = cursor.getInt(index_Person);
String strBody = cursor.getString(index_Body);
String longDate = cursor.getString(index_Date);
int intType = cursor.getInt(index_Type);
sms.add("\n"+strBody+"\n");
}while (cursor.moveToNext());
if (!cursor.isClosed()){
cursor.close();
cursor=null;
}
}
}catch (SQLiteException e){
Log.d("SQLiteException",e.getMessage());
}
return sms;
}
在这一行中我定义了我想使用的号码
Cursor cursor=getContentResolver().query(uri,projection,"address='+918870346164'",null,null);
在这里,我必须通过 TextView
从用户那里获取号码。
请帮忙
我认为你需要了解 Android Training
但无论如何EditText getText()
EditText et = (EditText) findViewById(R.id.edit_text_id);
String address="address='"+et.getText().toString()+"'";
cursor=getContentResolver().query(uri,projection,address,null,null);