1

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.

Piscean
  • 3,069
  • 12
  • 47
  • 96
  • What's it locking up on? Where does the debugger stop? – jsmith Jun 27 '12 at 14:03
  • well now its working with only difference that listView is not updated when i click on yes in delete alert. But when i go back and come on that listView then its updated. any idea how can i refresh my listView right after when i click yes button of alert? – Piscean Jun 27 '12 at 14:30

1 Answers1

1

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.

             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";
                        }

Try to debug and reduce code and loop on this click ...

LuminiousAndroid
  • 1,557
  • 4
  • 18
  • 28
  • well now its working with only difference that listView is not updated when i click on yes in delete alert. But when i go back and come on that listView then its updated. any idea how can i refresh my listView right after when i click yes button of alert? – Piscean Jun 27 '12 at 14:30