使用抛出 OutofMemoryError 的 SharedPreferences 加载列表视图
Loading listview using SharedPreferences throwing OutofMemoryError
我在 mainactivity 中使用了 edittext 和 button,单击时我想将文本添加到列表视图中,它确实正确。
我还使用 sharedpreferences 保存它,这样当我返回主活动或重新启动应用程序时,它会自动加载列表视图,但是在单击按钮时,我会在这一行出现 OutOfMemory 错误:
arrayCars.add(new Car(pred.getString("Value["+i+"]", ""),"green",3455));
列表视图代码:
ArrayList<Car> arrayCars;
ListView listViewCars;
String venName;
SharedPreferences pred;
SharedPreferences.Editor spEditor;
static int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayCars=new ArrayList<Car>();
System.out.println("count is"+count);
pred = getSharedPreferences("place", Context.MODE_PRIVATE);
spEditor = pred.edit();
count = pred.getInt("countd",count);
if(count > 0){
for(int i = 0; i < count; i++){
arrayCars.add(new Car(pred.getString("Value["+i+"]", ""),"green",3455));
count++;
}
}
listViewCars = (ListView) findViewById(R.id.list_cars);
ListCarsAdapter adapter = new ListCarsAdapter(this, arrayCars);
listViewCars.setAdapter(adapter);
listViewCars.setOnItemClickListener(this);
try{
Bundle bundle = getIntent().getExtras();
venName = bundle.getString("r");
}catch(Exception e){}
if(venName!=null){
spEditor.putString("Value["+count+"]", venName);
spEditor.apply();
arrayCars.add(new Car(pred.getString("Value["+count+"]", ""),"red",3444));
count += 1;
spEditor.putInt("countd", count);
adapter.notifyDataSetChanged();
}
}
PS: 我正在使用自定义列表视图
删除 count++;
在增加 i 的同时增加 count 变量,因此 for 循环将永远不会停止
count = pred.getInt("countd",count);
if(count > 0){
for(int i = 0; i < count; i++){
arrayCars.add(new Car(pred.getString("Value["+i+"]", ""),"green",3455));
count++;
}
}
在上面的代码中,你每次都将计数增加 1,因此它永远不会结束循环将重复直到它抛出异常。
为了避免删除那个count++;
希望对您有所帮助。
我在 mainactivity 中使用了 edittext 和 button,单击时我想将文本添加到列表视图中,它确实正确。
我还使用 sharedpreferences 保存它,这样当我返回主活动或重新启动应用程序时,它会自动加载列表视图,但是在单击按钮时,我会在这一行出现 OutOfMemory 错误:
arrayCars.add(new Car(pred.getString("Value["+i+"]", ""),"green",3455));
列表视图代码:
ArrayList<Car> arrayCars;
ListView listViewCars;
String venName;
SharedPreferences pred;
SharedPreferences.Editor spEditor;
static int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayCars=new ArrayList<Car>();
System.out.println("count is"+count);
pred = getSharedPreferences("place", Context.MODE_PRIVATE);
spEditor = pred.edit();
count = pred.getInt("countd",count);
if(count > 0){
for(int i = 0; i < count; i++){
arrayCars.add(new Car(pred.getString("Value["+i+"]", ""),"green",3455));
count++;
}
}
listViewCars = (ListView) findViewById(R.id.list_cars);
ListCarsAdapter adapter = new ListCarsAdapter(this, arrayCars);
listViewCars.setAdapter(adapter);
listViewCars.setOnItemClickListener(this);
try{
Bundle bundle = getIntent().getExtras();
venName = bundle.getString("r");
}catch(Exception e){}
if(venName!=null){
spEditor.putString("Value["+count+"]", venName);
spEditor.apply();
arrayCars.add(new Car(pred.getString("Value["+count+"]", ""),"red",3444));
count += 1;
spEditor.putInt("countd", count);
adapter.notifyDataSetChanged();
}
}
PS: 我正在使用自定义列表视图
删除 count++;
在增加 i 的同时增加 count 变量,因此 for 循环将永远不会停止
count = pred.getInt("countd",count);
if(count > 0){
for(int i = 0; i < count; i++){
arrayCars.add(new Car(pred.getString("Value["+i+"]", ""),"green",3455));
count++;
}
}
在上面的代码中,你每次都将计数增加 1,因此它永远不会结束循环将重复直到它抛出异常。
为了避免删除那个count++;
希望对您有所帮助。