我有一个ListView
,它有一个自定义适配器,PopupMenu
在列表项上提供了一些选项,包括重命名选项。重命名列表项后,我想刷新ListView
并显示重命名的列表项。
以下是我的代码。
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;
}
}
});
}
}
}
}
}
答案 0 :(得分:4)
您应该重新填充文件夹列表,然后调用Notifydatasetchanged。
fileNames = fileFunctions.listFileNames(Environment.getExternalStorageDirectory() + "/Documents/Files");
for(int i = 0; i < fileNames.length; i++){
folderList.add(new FolderBean(fileNames[i], "text"));
}