Update the ListView after renaming a ListView item within a Popup Menu in a BaseAdapter in Android

851 views Asked by At

I have a ListView which has a custom Adapter and a PopupMenu giving some options on the list item, including a rename option. After renaming the list item I want to refresh the ListView and display the renamed list item also.
Following is my code.

DocumentsFragment

public class DocumentsFragment extends Fragment{
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_my_doc, container, false);       

        ArrayList<FolderBean> folderList = new ArrayList<FolderBean>();
        fileNames = fileFunctions.listFileNames(Environment.getExternalStorageDirectory() + "/Documents/Files");

        for(int i = 0; i < fileNames.length; i++){
            folderList.add(new FolderBean(fileNames[i], "text"));
        }

        listView = (ListView) rootView.findViewById(R.id.myDocList);
        CustomFolderListAdapter cAdapter = new CustomFolderListAdapter(getActivity(), folderList);
        listView.setAdapter(cAdapter);
        listView.setOnItemClickListener(this);          
        listView.setOnItemLongClickListener(this);

        return rootView;
    }
}

CustomFolderListAdapter

public class CustomFolderListAdapter extends BaseAdapter {

    private ArrayList<FolderBean> folderList;
    private Context context;
    private FolderBean folderBean;

    public CustomFolderListAdapter(Context applicationContext, ArrayList<FolderBean> questionForSliderMenu) {   
        super();
        this.context = applicationContext;
        this.folderList = questionForSliderMenu;    
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        folderBean = new FolderBean();
        folderBean = folderList.get(position);

        if (convertView == null) {  
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.folder_popup_item, null);
        }

        TextView fileName = (TextView) convertView.findViewById(R.id.file_name);
        ImageView fileImage = (ImageView) convertView.findViewById(R.id.file_icon);
        ImageView fileOptions = (ImageView) convertView.findViewById(R.id.file_options_icon);

        fileOptions.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.file_options_icon:
                         PopupMenu popup = new PopupMenu(context, view);
                         popup.getMenuInflater().inflate(R.menu.clipboard_popup,  popup.getMenu());
                         popup.show();
                         popup.setOnMenuItemClickListener(new  PopupMenu.OnMenuItemClickListener() {
                             @Override
                             public boolean onMenuItemClick(MenuItem item) {

                                 switch (item.getItemId()) {
                                 case R.id.file_rename_menu:


                                    final String filename = folderList.get(position).getName();
                                    final String ext =   filename.substring(filename.indexOf(".") + 1);

                                    AlertDialog.Builder builderR = new AlertDialog.Builder(context);
                                    builderR.setTitle("Rename file");
                                    builderR.setCancelable(true);

                                    final EditText input = new EditText(context);
                                    input.setText(filename.substring(0, filename.indexOf(".")));
                                    builderR.setView(input);

                                    builderR.setPositiveButton("Rename", new DialogInterface.OnClickListener() {

                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            if(input.getText().toString().equals("")){
                                                UtilClass.getInstance().displayToast(context, "Please enter a name for the file", Toast.LENGTH_LONG);
                                            }
                                            else{
                                                File from = new File(Environment.getExternalStorageDirectory() + "/Documents/Files/" + filename);

                                                File to = new File(Environment.getExternalStorageDirectory() + "/Documents/Files/" + input.getText().toString() + "." + ext);

                                                boolean renamed = from.renameTo(to);

                                                if(renamed){
                                                    System.out.println("The position is " + position);
                                                    /** I have to do it here, don't I? How should I update the listview with the renamed file name?     **/                                 
                                                    notifyDataSetChanged();
                                                }                                               
                                            }
                                        }                                       
                                    });

                                    builderR.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                        }
                                    });

                                    AlertDialog alertR = builderR.create();
                                    alertR.show();

                                    break;
                                 }
                             }
                         });
                }
            }
        }

    }
}
1

There are 1 answers

0
Y2K On BEST ANSWER

You Should re-populate the folderlist then call Notifydatasetchanged.

fileNames = fileFunctions.listFileNames(Environment.getExternalStorageDirectory() + "/Documents/Files");

    for(int i = 0; i < fileNames.length; i++){
        folderList.add(new FolderBean(fileNames[i], "text"));
    }