我需要删除listview
发送的短信时...我收到此错误:java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
当broadcast receiver
尝试发送短信时,但是当我点击按钮时"调度"它被添加到list view
......现在我告诉你我的代码!
先谢谢大家!
我希望你能帮助我!
PS:如果我添加了这个方法:
@Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
//Toast.makeText(MainActivity.this, "Item has been added into your list", Toast.LENGTH_LONG)
// .show();
dataSms.remove(position);
adapter.notifyDataSetChanged();
}
它有效,但我希望在发送短信后自动删除第一项!
主要活动:
public class MainActivity extends Activity {
private static final int PERMS_REQUEST_CODE = 123;
private static final int REQUEST_CODE = 1;
TextView textViewTime;
TimePicker timePicker;
Button button;
int hour;
int minutee;
//int hour;
//int minute;
final int TIME_DIALOG_ID = 999;
public String sPhone,sSms;
public EditText etPhone,etSms;
public Button bStart,bCancel,bTimeSelect,bPhone;
ArrayList<Sms> dataSms;
ListView listView;
public static CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, 1);
listView = (ListView) findViewById(R.id.listView1);
dataSms = new ArrayList<Sms>();
adapter = new CustomAdapter(dataSms, getApplicationContext());
listView.setAdapter(adapter);
//Button b=(Button) findViewById(R.id.button1);
//Button button=(Button) findViewById(R.id.button);
bStart = (Button)findViewById(R.id.bStart);
//bCancel = (Button)findViewById(R.id.bCancel);
bTimeSelect = (Button)findViewById(R.id.bTime);
bPhone = (Button)findViewById(R.id.bCPhone);
bStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* if (hasPermissions()) {
// our app has permissions.
scheduleAlarm(v);
} else {
//our app doesn't have permissions, So i m requesting permissions.
requestPerms();
}*/
scheduleAlarm(v);
}
});
bTimeSelect.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onClick(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(),"TimePicker");
}
});
//};
bPhone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Uri uri = Uri.parse("content://contacts");
Intent intent = new Intent(Intent.ACTION_PICK, uri);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_CODE);
}
});
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("ValidFragment")
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{
//TimePicker timePicker;
//int hour;
//int minutee;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
//Use the current time as the default values for the time picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
//Create and return a new instance of TimePickerDialog
return new TimePickerDialog(getActivity(),this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
//onTimeSet() callback method
@TargetApi(Build.VERSION_CODES.M)
public void onTimeSet(TimePicker view, int hourOfDay, int minute){
hour=hourOfDay;
minutee=minute;
}
}
@TargetApi(Build.VERSION_CODES.M)
public void scheduleAlarm(View V) {
EditText et1 = (EditText) findViewById(R.id.etPhone);
EditText et2 = (EditText) findViewById(R.id.etSms);
String name = et1.getText().toString();
String sms = et2.getText().toString();
//adapter.add(new Sms(name, sms));
PendingIntent pendingIntent;
//Long time= new GregorianCalendar().getTimeInMillis()+24*60*60*1000;
Long time = new GregorianCalendar().getTimeInMillis() + 10000;
//Long time= System.currentTimeMillis() + 10000;
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Bundle bundle = new Bundle();
bundle.putCharSequence("Number", name);
bundle.putCharSequence("Message", sms);
intentAlarm.putExtras(bundle);
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minutee);
//alarmManager.set(AlarmManager.RTC_WAKEUP, time, PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
Toast.makeText(this, "Messaggio programmato!", Toast.LENGTH_LONG).show();
adapter.add(new Sms(name, sms));
}
}
广播接收器:
public class AlarmReceiver extends BroadcastReceiver {
private Intent intent;
String value1 = null;
String value2 = null;
ListView lv;
ArrayList<String> al;
ArrayAdapter<String> aa;
ArrayList<Sms> dataSms;
public static CustomAdapter adapter;
ListView listView;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
dataSms = new ArrayList<Sms>();
adapter = new CustomAdapter(dataSms, context);
//dataSms.remove(0);
//adapter.notifyDataSetChanged();
//adapter.remove(adapter.getItem(0));
String phoneNumberReciver = "";
String message = "blablabla";
String name = intent.getStringExtra("Number");
String sms = intent.getStringExtra("Message");
/*String SPhone =i.getStringExtra("exPhone");
String SSms = i.getStringExtra("exSmS");*/
//android.telephony.SmsManager sms= SmsManager.getDefault();
//sms.sendTextMessage(phoneNumberReciver, null, message, null, null);
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(name, null, sms, null, null);
Toast.makeText(context, "Messaggio inviato!", Toast.LENGTH_LONG).show();
dataSms.remove(0);
adapter.notifyDataSetChanged();
/* @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
liste.remove(position);
adapter.notifyDataSetChanged();
}*/
// public Intent getIntent() {
// return intent;
// }
}
}
答案 0 :(得分:2)
错误发生在stacktrace中:
dataSms = new ArrayList<Sms>();
// ...
dataSms.remove(0);
您永远不会在列表中添加项目,但您想要删除一个
答案 1 :(得分:0)
您的ArrayList中没有数据。这就是堆栈跟踪说大小为0的原因。如果大小为0,那么如何从ArrayList中删除元素。
删除此行dataSms.remove(0);
答案 2 :(得分:0)
请检查您声明的ArrayList。我想在声明你没有在数组列表中添加任何内容之后。
0
我建议你逐行调试。 在尝试从arrayList中删除值之前。尝试检查其大小是否为零或是否为空。如果没有,那么您可以尝试下一步。
dataSms = new ArrayList<Sms>();
请投票并标记解决方案是否有用。
<强>谢谢!强>