我想处理EditText和我的listView中的按钮发送短信。 到目前为止,电话号码是由用户输入的。所以,我做了一个适配器,如下:处理按钮和EditText发送短信
public class ListViewStudentWithDateAdapter extends ArrayAdapter<ListViewStudentWithDate> {
private Context mContext;
private ArrayList<ListViewStudentWithDate> mData;
private EditText Parent_Number ;
public ListViewStudentWithDateAdapter (Context mContext, ArrayList<ListViewStudentWithDate> mData)
{
super(mContext, R.layout.student_list_with_date_shape, mData);
this.mContext = mContext;
this.mData = mData;
}
public int getCount() {
return mData.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.student_list_with_date_shape, null);
}
TextView Name = (TextView) convertView.findViewById(R.id.Name);
Name.setText(mData.get(position).getStudent_name());
TextView GRADE = (TextView) convertView.findViewById(R.id.GRADE_ID);
GRADE.setText(mData.get(position).getGrade_id());
TextView CLASS = (TextView) convertView.findViewById(R.id.CLASS_ID);
CLASS.setText(mData.get(position).getCLass_id());
Parent_Number = (EditText) convertView.findViewById(R.id.Father_number);
Button Send_Father = (Button) convertView.findViewById(R.id.Send_Father);
Send_Father.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("Send SMS", "");
String phoneNo = Parent_Number.getText().toString();
String sms = "HELLO World" ;
try {
// Get the default instance of the SmsManager
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo,
null,
sms,
null,
null);
Toast.makeText(getContext(), "Your sms has successfully sent!",
Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getContext(),"Your sms has failed...",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
});
return convertView;
}
}
它似乎并不奏效,因为它总是给我的失败消息。任何想法 ??
有你声明** <使用许可权的android:NAME =”清单文件中的android.permission.SEND_SMS“/> **权限? –
你收到什么信息?发布您的日志。 – Pankaj
我给了权限 – ama989