我将这段代码输入到我的应用程序中,我需要当您在弹出对话框中单击“确定”或“ Borrar”时,将ArrayList保存为“ al”,以便当我关闭应用程序并再次打开它时,列表中包含先前的项目,我非常努力,但是我不知道为什么,我是6天前开始使用Java的,所以我没有太多想法,谢谢!
public class Iniciado extends Activity {
TextView tv3;
ListView listView;
final Context context = this;
final ArrayList<String> al = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.iniciado);
conseguirdatos();
tv3=(TextView)findViewById(R.id.tv3);
Bundle bolsar = getIntent().getExtras();
tv3.setText("Bienvenido "+ bolsar.getString("NombreKey"));
listView = (ListView)findViewById(R.id.list);
// Defined Array values to show in ListView
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, al);
// Assign adapter to ListView
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ListView Clicked item index
final int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
// Make Dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.cambiar);
dialog.setTitle("Editar evento");
// set the custom dialog components - text, image and button
final EditText editar = (EditText)dialog.findViewById(R.id.eteditar);
editar.setText(itemValue);
Button okBtn = (Button) dialog.findViewById(R.id.okbtn);
Button borrarBtn = (Button) dialog.findViewById(R.id.borrarbtn);
// if button is clicked, close the custom dialog
okBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String recuperado = editar.getText().toString();
al.set(itemPosition, recuperado);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Iniciado.this, android.R.layout.simple_list_item_1, android.R.id.text1, al);
adapter.notifyDataSetChanged();
dialog.dismiss();
}
});
borrarBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
al.remove(itemPosition);
adapter.notifyDataSetChanged();
dialog.dismiss();
}
});
dialog.show();
}
});
}
private void conseguirdatos() {
if(al.size() == 0){
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
}
}
}
最佳答案
这就是你想要的:
public static ArrayList<String> readList(Context c){//
try{
FileInputStream fis = c.openFileInput("NAME");
ObjectInputStream is = new ObjectInputStream(fis);
ArrayList<String> list = (ArrayList<String>)is.readObject();
is.close();
return list;
}catch(Exception ex){
ex.printStackTrace();
return null;
}
}
public static void writeList(Context c, ArrayList<String> list){
try{
FileOutputStream fos = c.openFileOutput("NAME", Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(list);
os.close();
}catch(Exception ex){
ex.printStackTrace();
}
}