2016-07-16 98 views
0

我有自定义的事件与他们的日期列表视图。如果该用途选择列表上的特定项目,则我的程序将询问用户是否想要提醒该事件。我遇到了循环事件的问题。例如,对于位置1,如果我将提醒设置为下午2:15和位置2,则会将提醒设置为2:30。第二个列表的通知将会出现,而忽略第一个列表。我希望这两个通知都能在各自的时间显示。Android列表视图循环

有人可以找出并帮助我如何循环列表,所以我可以设置列表上的任何项目的提醒。

请检查OnClickResponse()方法,这是我创建闹钟的地方。

'public class Product extends AppCompatActivity implements View.OnClickListener { 
private List<list> myList = new ArrayList<list>(); 
private PendingIntent pendingIntent; 

private void fill_ListView() {

ArrayAdapter<list> listArrayAdapter = new myListAdapter();

ListView list = (ListView) findViewById(R.id.product_View);

list.setAdapter(listArrayAdapter); 
 
 } private void clickResponse() {

ListView l = (ListView) findViewById(R.id.product_View);

if(l!=null){

l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
 
 @Override

public void onItemClick(AdapterView<?> parent, final View viewClicked,
 final int position, long id) {
 
 

AlertDialog.Builder alert = new AlertDialog.Builder(
 Product.this);

alert.setTitle("Reminder!!");

alert.setMessage("Do you want to be reminded of the expiry date of this product?”);

alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
 

@Override

public void onClick(DialogInterface dialog, int which) {
 
 Calendar calendar = Calendar.getInstance(); // checking if first item is clicked then a reminder will be set for some date

if(position==0){
 calendar.set(Calendar.MONTH, 6);

calendar.set(Calendar.YEAR, 2016);
 calendar.set(Calendar.DAY_OF_MONTH, 16);
 
 calendar.set(Calendar.HOUR_OF_DAY, 04);
 calendar.set(Calendar.MINUTE, 38);
 calendar.set(Calendar.SECOND, 0);
 calendar.set(Calendar.AM_PM,Calendar.AM);
 Intent myIntent = new Intent(Product.this, MyReceiver.class);
 pendingIntent = PendingIntent.getBroadcast(Product.this, 0,myIntent,0);
 
 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
 alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
 
 } if(position==1){

calendar.set(Calendar.MONTH, 6);
 calendar.set(Calendar.YEAR, 2016);
 calendar.set(Calendar.DAY_OF_MONTH, 16);
 
 calendar.set(Calendar.HOUR_OF_DAY, 04);
 calendar.set(Calendar.MINUTE, 39);
 calendar.set(Calendar.SECOND, 40);
 calendar.set(Calendar.AM_PM,Calendar.AM);
 Intent myIntent = new Intent(Product.this, MyReceiver.class);
 pendingIntent = PendingIntent.getBroadcast(Product.this, 0,myIntent,0);
 
 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
 alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
 
 }
 dialog.dismiss();
 }
 

});
 

alert.setNegativeButton("NO", new DialogInterface.OnClickListener() {
 @Override

public void onClick(DialogInterface dialog, int which) {
 
 dialog.dismiss();

}

});
 

alert.show();
 

}

});

}

}


private class myListAdapter extends ArrayAdapter<list> {

public myListAdapter() {

// what objects are going in there and how are they going to look
 super(Product.this, R.layout.custom_layout, myList);

}
 

@Override

public View getView(int position, View convertView, ViewGroup parent) {


View itemView = convertView;

if (itemView == null) {

itemView = getLayoutInflater().inflate(R.layout.custom_layout, parent, false);
 }
 list current_list = myList.get(position);
 
 
 ImageView imageView = (ImageView) itemView.findViewById(R.id.id_image);
 imageView.setImageResource(current_list.getIconId());
 

// getting the title of the event

TextView titleTxt = (TextView) itemView.findViewById(R.id.id_event);
 titleTxt.setText(current_list.getName());
 
 
 // getting the description
 //
TextView desp = (TextView) itemView.findViewById(R.id.id_desp);
 // desp.setText(current_list.getDetails());

EditText text = (EditText)itemView.findViewById(R.id.id_desp);
 text.setText(current_list.getDetails());
 
 
 TextView date = (TextView) itemView.findViewById(R.id.id_date);
 date.setText(current_list.getDate());
 

return itemView;

}
 
 

}
在此行中输入代码在这里”

回答

0

问题在于:

pendingIntent = PendingIntent.getBroadcast(Product.this, 0,myIntent,0); 

第二个参数代表requestCode和它必须是不同的警报是唯一的。您每次都将其设置为0,因为之前的警报将被覆盖。

+0

我将第二个if语句的第二个参数更改为1,但现在第一个正常工作,但第二个闹钟甚至没有显示 – timmy24565

+0

您需要为每个闹钟保持唯一。 –

+0

请你解释一下。谢谢。第一个出现,第二个出现,但没有分开。当第二次报警被调用时,第一个报警仍然被覆盖 – timmy24565