Search code examples
androidlistviewarraylistandroid-arrayadapter

Android list view looping


I have customized listview of events with their dates. If the use selects a specific item on the list then my program will ask if the user want to be reminded of that event. I am having problem looping the events. For example, for position 1,if I set the reminder to be at 2:15pm and for position 2, I set reminder to be at 2:30. The notification of second list will appear ignoring the first one. I want both of these notifications to show at their respective time.

Can someone find out and help me on how to loop the list so I am able to set reminders on any item on the list.

Please check the OnClickResponse() method, that's where I am creating an alarm.

'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;

}
 
 

}
enter code here'


Solution

  • The problem lies in this line:

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

    The second parameter stands for requestCode and it has to be unique for different alarms. You are setting it to 0 every time because of which the previous alarm gets overridden.