How to delete items from the list in Fragments?

advertisements

I want to delete the items from the fragments. When long click on the items than a action bar is changed and a delete button appears. I click on the delete button The item seems to be deleted from the list view. But when I restart the app the item appears again.

public class ReadFragment extends Fragment {
    ListView spinner;
    TextView empty;
    String selected;
    List<String> list;
    String[] filenames;

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

        View rootView = inflater.inflate(R.layout.fragment_read, container,
                false);
        spinner = (ListView) rootView.findViewById(R.id.spinner2);

        this.spinner.setEmptyView(rootView.findViewById(R.id.emptyElement));
        getFilenames();

    getactionbar();

        return rootView;

    }

    private void getFilenames() {
        // TODO Auto-generated method stub
        filenames = getActivity().fileList();
        list = new ArrayList<String>();
        for (int i = 0; i < filenames.length; i++) {
            // Log.d("Filename", filenames[i]);
            list.add(filenames[i]);
        }
        ArrayAdapter<String> filenameAdapter = new ArrayAdapter<String>(
                getActivity(), android.R.layout.simple_list_item_1,
                android.R.id.text1, list);

        spinner.setAdapter(filenameAdapter);
        spinner.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                selected = ((TextView) view).getText().toString();

                Intent intent = new Intent(getActivity(), ReadData.class);
                intent.putExtra("key", selected);

                startActivity(intent);

            }
        });

    }
    private void getactionbar() {
        // TODO Auto-generated method stub
        spinner.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        spinner.setMultiChoiceModeListener(new MultiChoiceModeListener() {

            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position,
                                                  long id, boolean checked) {
                // Here you can do something when items are selected/de-selected,
                // such as update the title in the CAB
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                // Respond to clicks on the actions in the CAB
                switch (item.getItemId()) {
                    case R.id.ic_action_discard:

                        deletefiles();

                        Toast.makeText(getActivity(), "Deleted", Toast.LENGTH_LONG).show();

                        mode.finish(); // Action picked, so close the CAB
                        return true;
                    default:
                        return false;
                }
            }

            private void deletefiles() {
            // TODO Auto-generated method stub
               SparseBooleanArray checkedItemPositions = spinner.getCheckedItemPositions();
                 int itemCount = spinner.getCount();

                 for(int i=itemCount-1; i >= 0; i--){
                     if(checkedItemPositions.get(i)){
                         filenameAdapter.remove(list.get(i));
                     }
                 }
                 checkedItemPositions.clear();
                filenameAdapter.notifyDataSetChanged();

        }   

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                // Inflate the menu for the CAB
                MenuInflater inflater = mode.getMenuInflater();
                inflater.inflate(R.menu.context, menu);
                return true;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                // Here you can make any necessary updates to the activity when
                // the CAB is removed. By default, selected items are deselected/unchecked.
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                // Here you can perform updates to the CAB due to
                // an invalidate() request
                return false;
            }
        });

    }
}

Can anyone help me deleting the item forever, I mean once it is deleted it should not appear in the listview again when the app is relaunched.


Try, tries it worked here

final Button btnRemove = (Button) findViewById(R.id.button1);
    btnRemove.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            lst.remove(adapter.getItem(2));
            adapter.notifyDataSetChanged();
        }
    });;