mercredi 22 avril 2015

App crash on using delete query in list view in fragment

I am working on call blocker project, now I' am showing the blocked sms in list view. When i want to delete any message then it give me alert dialog to delete this, now when i click on its button it'll execute the query...Then on this point it crashes. I don't know what the problem is with it. Here is my code.

public class BlockedSmsList extends Fragment {
        ListView list;
        private DbAdapterSmsLog mDbAdapter;
        private ArrayList<String> numberList = null;
        ArrayList<String> msgBody;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.block_backup, container,
                    false);
            numberList = new ArrayList<String>();
            msgBody = new ArrayList<String>();
            mDbAdapter = new DbAdapterSmsLog(getActivity().getApplicationContext());
            mDbAdapter.open();

            list = (ListView) rootView.findViewById(R.id.call_block_listview);
            displayLits();
            list.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,final int pos,
                        long arg3) {
                    AlertDialog.Builder adb=new AlertDialog.Builder(getActivity());
                    adb.setTitle("Please Confirm");
                    adb.setMessage("Are you sure to delete?");
                    adb.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            mDbAdapter.deleteReminder(pos);
                            mDbAdapter.close();
                            displayLits();
                        }
                    });
                    adb.setNeutralButton("Cencel", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            arg0.cancel();
                        }
                    });
                    adb.setNegativeButton("Delete All", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            mDbAdapter.deleteAllNumber();
                            mDbAdapter.close();
                            displayLits();
                        }
                    });
                    adb.show();
                }
            });

            return rootView;
        }

        public void displayLits() {

            Cursor c = mDbAdapter.fetchAllReminders();
            numberList.clear();
            c.moveToFirst();
            if (c.moveToFirst()) {

                while (c.isAfterLast() == false) {

                    String name = c.getString(c
                            .getColumnIndex(DbAdapterSmsLog.KEY_MODE));
                    String body = c.getString(c
                            .getColumnIndex(DbAdapterSmsLog.KEY_TITLE));
                    numberList.add(name);
                    msgBody.add(body);
                    c.moveToNext();
                }
            }
            SmsCustomAdopter adapter=new SmsCustomAdopter(getActivity().getApplicationContext(), numberList,msgBody);
            list.setAdapter(adapter);
        }

    }

Aucun commentaire:

Enregistrer un commentaire