I am trying to send one item of a list on being long clicked to another activity's list.But the second activity i.e MySchedule doesnt update beyond one item. Here's My code
Activity from where i am sending the string (didnt added the code of string)
public class CloudEvents extends AppCompatActivity {
static int scheduleId = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
new AlertDialog.Builder(CloudEvents.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Save Event")
.setMessage("Do you want to save this event into your schedule?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
scheduleId++;
Toast.makeText(CloudEvents.this,"Saved",Toast.LENGTH_LONG).show();
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.yatin.whatshappeningdtu", Context.MODE_PRIVATE);
sharedPreferences.edit().putString("CloudEvent",listView.getItemAtPosition(position).toString()).apply();
Intent i = new Intent(CloudEvents.this,MySchedule.class);
startActivity(i);
//myArrayAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("No",null)
.show();
return true;
}
});
Activity Receiving the string and making a list
public class MySchedule extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_schedule);
final ArrayList<String> schedule = new ArrayList<>();
final ListView scheduleListView = (ListView)findViewById(R.id.scheduleListView);
String key = "CloudEvent";
String myEvent ="";
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.yatin.whatshappeningdtu", Context.MODE_PRIVATE);
if(sharedPreferences.contains(key))
{
myEvent = sharedPreferences.getString(key,"");
schedule.add(CloudEvents.scheduleId,myEvent);
}
final ArrayAdapter myArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,schedule);
scheduleListView.setAdapter(myArrayAdapter);
scheduleListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
new AlertDialog.Builder(MySchedule.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Are you sure ?")
.setMessage("Do you want to delete this note")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
schedule.remove(position);
myArrayAdapter.notifyDataSetChanged();
CloudEvents.scheduleId--;
}
})
.setNegativeButton("No",null)
.show();
return true;
}
});
}
}
(after adding one item)
Error:Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.add(ArrayList.java:457)
at com.yatin.whatshappeningdtu.MySchedule.onCreate(MySchedule.java:35)
Being racking my brain for hours now.Please Help Thanks !
When you add item first time, your CloudEvents.scheduleId
is set to 0 from -1. Suppose your string is "FirstEvent"
that you save in CloudEvent
key of sharedpreference, and then In MySchedule
activity it is added in 0 position of schedule
arraylist, and it works fine.
Now when you come back to CloudEvents
activity, your CloudEvents.scheduleId
is 0 because it's statica variable, and you add another item let's say "SecondEvent"
, so CloudEvents.scheduleId
will change from 0 to 1, and you are saving "SecondEvent"
string in CloudEvent
key again in sharedpreference, so that previous value "FirstEvent"
will override with "SecondEvent"
that means in MySchedule
activity you will only get "SecondEvent"
from sharedpreference, but you are adding this in 1st position of schedule
arraylist and 0th position of schedule
arraylist will be left null. Now you are passing this arraylist, with null value in 0th position, in Listview adapter, that's way it is throwing IndexOutOfBoundsException
exception. To solve this issue, you can maintain one ArrayList<String>
.In CloudEvents
activity make following changes.
private ArrayList<String> eventList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState){
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.yatin.whatshappeningdtu", Context.MODE_PRIVATE);
Collections.addAll(eventList, prefManager.getString("CloudEvent", "").split(","));
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
new AlertDialog.Builder(CloudEvents.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Save Event")
.setMessage("Do you want to save this event into your schedule?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
scheduleId++;
eventList.add(listView.getItemAtPosition(position).toString());
Toast.makeText(CloudEvents.this, "Saved", Toast.LENGTH_LONG).show();
Intent i = new Intent(CloudEvents.this, MySchedule.class);
i.putStringArrayListExtra("EventList", eventList);
startActivity(i);
//myArrayAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("No", null)
.show();
return true;
}
});
}
Now in MySchedule
activity make following changes.
public class MySchedule extends AppCompatActivity {
ArrayList<String> schedule = new ArrayList<>();
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_schedule);
sharedPreferences = getApplicationContext().getSharedPreferences("com.yatin.whatshappeningdtu", Context.MODE_PRIVATE);
schedule = getIntent().getStringArrayListExtra("EventList");
ListView scheduleListView = (ListView)findViewById(R.id.scheduleListView);
final ArrayAdapter myArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,schedule);
scheduleListView.setAdapter(myArrayAdapter);
scheduleListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
new AlertDialog.Builder(MySchedule.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Are you sure ?")
.setMessage("Do you want to delete this note")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
schedule.remove(position);
myArrayAdapter.notifyDataSetChanged();
String events = "";
for(int i=0; i<schedule.size(); i++){
events = events + schedule.get(i);
if(i != (schedule.size() - 1)){
events = events + ",";
}
}
sharedPreferences.edit().putString("CloudEvent", events).apply();
}
})
.setNegativeButton("No",null)
.show();
return true;
}
});
}
}