I want to delete an array index from an array, then reload listView with my new array and then write a string to an existing file in android. I am using following code:
OnItemLongClickListener longClickListener = new OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
itemPosition = position;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Warning");
alertDialogBuilder
.setMessage("Are you sure?")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
ArrayList<String> itemsList = new ArrayList<String>();
for(int i=0; i<items.length; i++){
itemsList.add(items[i]);
}
itemsList.remove(itemPosition);
items = new String[itemsList.size()];
for(int i=0; i<itemsList.size(); i++){
items[i] = itemsList.get(i);
}
adapter.notifyDataSetChanged();
String newData = "";
for(int i=0; i<items.length; i++){
newData = newData+items[i];
if(i < items.length-1){
newData = newData+"NEXTLINE";
}
}
// write string to existing file
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(newData.getBytes());
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
return true;
}
};
when i click yes on alert then my app doesn’t respond and after some time my phone ask to kill the app? Can anybody please tell me that whats wrong with this code? thanks in advance.
the problem is in following snippt of code… MIght be your list have a too lagre of size and you are looping it thrice and in between you are also refreshing on list.
Try to debug and reduce code and loop on this click …