Android列表视图循环

时间:2016-07-16 10:16:04

标签: android listview arraylist android-arrayadapter

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

有人可以找到并帮助我如何循环列表,以便我能够对列表中的任何项目设置提醒。

请检查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;

}
 
 

}
在此处输入代码&#39;

1 个答案:

答案 0 :(得分:0)

问题出在这一行:

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

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