如何在`ListView`中显示来自特定电话号码的消息?

时间:2015-05-17 14:22:08

标签: android listview sms

我必须在SMS中显示来自给定号码的ListView条消息。 我已经显示了我在程序中描述的数字的消息。 现在我必须使用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从用户那里获取号码。 请帮忙

1 个答案:

答案 0 :(得分:0)

我认为你需要跟踪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);