我想在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;
}
}
它似乎不起作用,因为它总是给我失败的信息。有什么想法??
答案 0 :(得分:0)
检查你的清单是否持续:
<uses-permission android:name="android.permission.SEND_SMS" />
我认为你的电话号码是空的,调试它
答案 1 :(得分:0)
您最有可能获得IllegalArgumentException
,因为您传递给sendTextMessage()
的号码为空。 Parent_Number
是一个类成员,它将保留对getView()
调用创建的最后一个实例的引用,Parent_Number
不一定是您当前正在编辑的实例。
您需要在EditText
Send_Father
的代码上缓存Button
onClick()
,并在public View getView(final int position, View convertView, ViewGroup parent)
{
...
Parent_Number = (EditText) convertView.findViewById(R.id.Father_number);
Button Send_Father = (Button) convertView.findViewById(R.id.Send_Father);
Send_Father.setTag(Parent_Number);
Send_Father.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("Send SMS", "");
EditText parent = (EditText) view.getTag();
String phoneNo = parent.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;
}
方法中检索它及其文字:< / p>
public class LoginNarrowActivity extends BaseActivity implements LoginFragment.CallBacks {
@Override
protected void onCreate(Bundle savedState){
super.onCreate(savedState);
setContentView(R.layout.activity_login_narrow);
LoginFragment loginFragment = (LoginFragment)getSupportFragmentManager().findFragmentById(R.id.your_fragment_id);
loginFragment.setCallBacks(this);
}