我正在尝试发送列表中的一项被长点击到另一项活动的list.But,第二项活动,即MySchedule不会更新超过一项。这是我的代码
我从哪里发送字符串的活动(没有添加字符串的代码)
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;
}
});接收字符串并创建列表的活动
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;
}
});
}
}(添加一个项目后)
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)绞尽脑汁了几个小时,谢谢你的帮助,now.Please!
发布于 2017-02-16 19:41:24
第一次添加项目时,您的CloudEvents.scheduleId将从-1设置为0。假设您的字符串是保存在共享首选项的CloudEvent键中的"FirstEvent",然后在MySchedule活动中,它被添加到schedule数组列表的0位置,它工作得很好。
现在,当您返回到CloudEvents活动时,您的CloudEvents.scheduleId是0,因为它是statica变量,并且您添加了另一个项,比如说"SecondEvent",因此CloudEvents.scheduleId将从0更改为1,并且您将再次将"SecondEvent"字符串保存在共享首选项中的CloudEvent键中,因此以前的值"FirstEvent"将使用"SecondEvent"覆盖,这意味着在MySchedule活动中,您将只从共享首选项获得<代码>d14,但是您将此添加到<代码>d15数组列表的第1个位置,<代码>d16数组列表的第0个位置将为空。现在你在Listview适配器中传递这个数组列表,在第0个位置有空值,这就是它抛出IndexOutOfBoundsException异常的方式。要解决此问题,您可以维护一个ArrayList<String>.In CloudEvents活动,并进行以下更改。
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;
}
});
}现在,在MySchedule活动中进行以下更改。
公共类MySchedule扩展了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;
}
});
}
}发布于 2017-02-16 18:20:15
只需尝试commit()而不是apply()
apply()是在2.3中添加的,它提交时不返回指示成功或失败的布尔值。
如果保存有效,则commit()返回true,否则返回false。
还请确认用于读取数据的共享首选项实例与写入数据的实例相同。
https://stackoverflow.com/questions/42270682
复制相似问题