我通過arraylist在listview中獲取我的文件夾文件。 @點擊我重命名一個文件,但我的listview不刷新使用adapter.datasetnotifychanged。我正在使用自定義基礎適配器。任何可以告訴我如何自動刷新列表後,「重命名」到「重命名」文件。在使用基本適配器重命名文件後刷新lisview
Adapter.class
public class CustomAdapter extends BaseAdapter {
Context context;
ArrayList<String> countryList;
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, ArrayList<String> countryList) {
this.context = context;
this.countryList = countryList;
inflter = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
return countryList.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.list_items, null);
String hello = countryList.get(position);
Log.d("Hello",hello);
TextView country = (TextView) view.findViewById(R.id.textView);
country.setText(hello.substring(0, hello.lastIndexOf(")")+1));
return view;
}
Rename.class
File sdCardRoot = Environment.getExternalStorageDirectory();
yourDir = new File(sdCardRoot, "/MoreData/closed");
FilesInFolder = GetFiles(yourDir);
if(FilesInFolder!=null) {
customAdapter = new CustomAdapter(getActivity(), FilesInFolder);
lv.setAdapter(customAdapter);
customAdapter.notifyDataSetChanged();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final File file = new File(path.get(position));
value_new = FilesInFolder.get(position).toString();
File from = new File(root,String.valueOf(file.getName()));
File to = new File(root,String.valueOf(file.getName()).substring(0,file.getName().lastIndexOf(")")+1)+newencrypt);
from.renameTo(to);}
public ArrayList<String> GetFiles(File DirectoryPath) {
ArrayList<String> MyFiles = new ArrayList<String>();
File f = new File(String.valueOf(DirectoryPath));
f.mkdirs();
File[] files = f.listFiles();
if (files.length == 0)
return null;
else {
for (int i=0; i<files.length; i++)
MyFiles.add(files[i].getName());
}
return MyFiles;
}
private void getDir(String dirPath)
{
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
if(!file.isHidden() && file.canRead()){
path.add(file.getPath());
if(file.isDirectory()){
item.add(file.getName() + "/");
}else{
item.add(file.getName());
}
}
}
你需要調用notifyDataSetChanged()方法重命名文件之後。 –
試過!!!但沒有運氣:( –
請檢查renameTo()函數的返回值。如果它是true,那麼只有重命名成功。 –