我想将普通短信发送到具有不同消息的多个号码。在这里,我已将电话号码和消息检索到列表视图。当我单击发送按钮时,它应该逐行读取该列表视图中的所有行,并且应该逐个发送消息。任何人都可以帮助我。我知道这个编码用于向一个号码发送消息。但我想在列表视图中发送多个号码。
String phoneNo = user_name.toString();
String message = user_department.toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent.",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
这是我的列表数组适配器。
SQLiteDatabase db = dbhelper.getWritableDatabase();
Cursor mCursor=db.rawQuery("SELECT * FROM contacts WHERE department='"+label+"'", null);
userId.clear();
user_name.clear();
user_department.clear();
if (mCursor.moveToFirst()) {
do {
userId.add(mCursor.getString(mCursor.getColumnIndex(DBhelper.CONTACTS_COLUMN_ID)));
user_name.add(mCursor.getString(mCursor.getColumnIndex(DBhelper.CONTACTS_COLUMN_NAME)));
user_department.add(mCursor.getString(mCursor.getColumnIndex(DBhelper.CONTACTS_COLUMN_DEPARTMENT)));
} while (mCursor.moveToNext());
}
DisplayAdapter disadpt = new DisplayAdapter(home.this,userId, user_name, user_department);
lv.setAdapter(disadpt);
disadpt.notifyDataSetChanged();
mCursor.close();
答案 0 :(得分:1)
对于列表视图,数据由adapter
保存。所以你必须循环通过适配器项来发送消息。
与以下代码类似
for(int i=0;i<adapter.getCount();i++){
sendMsg(adapter.getItem(i).number);
}
此处Adapter
,getItem
返回给定位置的项目。我希望你有那个对象的电话号码。您希望发送消息的次数多次调用消息发送方法。 : - /
答案 1 :(得分:0)
Contract{
String userName ;
String userId ;
String user_department ;
String phoneNum ;
}
class DisplayAdapter extends BaseAdapter {
List<Contract> mDatas ;
DisplayAdapter(Context context, List<Contract> datas) {
mDatas = datas ;
}
// ......
}
SQLiteDatabase db = dbhelper.getWritableDatabase();
Cursor mCursor=db.rawQuery("SELECT * FROM contacts WHERE department='"+label+"'", null);
List<Contract> datas = new ArrayList<Contract>();
if (mCursor.moveToFirst()) {
do {
Contract newContract = new Contract();
newContract.userId = mCursor.getString(mCursor.getColumnIndex(DBhelper.CONTACTS_COLUMN_ID));
newContract.userName = mCursor.getString(mCursor.getColumnIndex(DBhelper.CONTACTS_COLUMN_NAME));
newContract.user_department = mCursor.getString(mCursor.getColumnIndex(DBhelper.CONTACTS_COLUMN_DEPARTMENT));
newContract.phoneNum = "set user's phone num";
datas.add(newContract);
} while (mCursor.moveToNext());
}
DisplayAdapter disadpt = new DisplayAdapter(Home.this, datas);
lv.setAdapter(disadpt);
disadpt.notifyDataSetChanged();
mCursor.close();
// button click and send msg.
for(Contract contractItem : datas) {
// send sms code method
}