Android 使用基本适配器重命名文件后刷新listview
我正在通过arraylist获取listview中的文件夹文件。@单击某个东西时,我重命名了一个文件,但我的listview并没有使用adapter.datasetnotifychanged刷新。我正在使用自定义的基本适配器。请告诉我如何在“重命名为”文件后自动刷新列表 适配器类Android 使用基本适配器重命名文件后刷新listview,android,listview,baseadapter,Android,Listview,Baseadapter,我正在通过arraylist获取listview中的文件夹文件。@单击某个东西时,我重命名了一个文件,但我的listview并没有使用adapter.datasetnotifychanged刷新。我正在使用自定义的基本适配器。请告诉我如何在“重命名为”文件后自动刷新列表 适配器类 public class CustomAdapter extends BaseAdapter { Context context; ArrayList<String> countryList; Layout
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;
}
公共类CustomAdapter扩展了BaseAdapter{
语境;
ArrayList国家列表;
更平坦的过滤器;
公共CustomAdapter(上下文应用程序上下文,ArrayList countryList){
this.context=上下文;
this.countryList=countryList;
inflter=(LayoutInflater.from(applicationContext));
}
@凌驾
public int getCount(){
返回countryList.size();
}
@凌驾
公共对象getItem(int i){
返回null;
}
@凌驾
公共长getItemId(int i){
返回0;
}
@凌驾
公共视图getView(内部位置、视图视图、视图组视图组){
视图=过滤器充气(R.layout.list\u项目,空);
字符串hello=countryList.get(位置);
Log.d(“你好”,你好);
TextView国家=(TextView)view.findViewById(R.id.TextView);
country.setText(hello.substring(0,hello.lastIndexOf(“)”)+1));
返回视图;
}
重命名.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());
}
}
}
文件sdCardRoot=Environment.getExternalStorageDirectory();
yourDir=新文件(sdCardRoot,“/MoreData/closed”);
fileinfolder=GetFiles(yourDir);
if(fileinfolder!=null){
customAdapter=新的customAdapter(getActivity(),fileInfolder);
lv.setAdapter(自定义适配器);
customAdapter.notifyDataSetChanged();
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
最终文件=新文件(path.get(position));
value_new=fileinfolder.get(position.toString();
filefrom=新文件(root,String.valueOf(File.getName());
File to=新文件(root,String.valueOf(File.getName()).substring(0,File.getName().lastIndexOf(“)”)+1)+newencrypt);
from.renameTo(to);}
公共ArrayList GetFiles(文件目录路径){
ArrayList MyFiles=新建ArrayList();
文件f=新文件(String.valueOf(DirectoryPath));
f、 mkdirs();
File[]files=f.listFiles();
如果(files.length==0)
返回null;
否则{
对于(int i=0;i调用onclick内部的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);
//after renaming it will refresh listview
customAdapter.notifyDataSetChanged();
}
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
最终文件=新文件(path.get(position));
value_new=fileinfolder.get(position.toString();
filefrom=新文件(root,String.valueOf(File.getName());
File to=新文件(root,String.valueOf(File.getName()).substring(0,File.getName().lastIndexOf(“)”)+1)+newencrypt);
由.更名为(至);;
//重命名后,它将刷新listview
customAdapter.notifyDataSetChanged();
}
在onItemClick()中还应调用自定义适配器.notifyDataSetChanged()
最后,只会重新绘制listview,它使用适配器知道要绘制什么。适配器使用ArrayList作为数据输入。如果在调用notifyDataSetChanged之前未更改此对象,listview将保持完全相同
若要反映列表中的更改,必须更改附加到适配器的数据。因此,应提供允许修改适配器数据的方法
例如,将此方法添加到CustomAdapter:
public void updateData(ArrayList<String> countryList) {
this.countryList = countryList;
notifyDataSetChanged();
}
public void updateData(ArrayList countryList){
this.countryList=countryList;
notifyDataSetChanged();
}
然后,单击某个项目时,相应地调整文件列表并更新适配器
更新:
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);
File sdCardRoot = Environment.getExternalStorageDirectory();
yourDir = new File(sdCardRoot, "/MoreData/closed");
FilesInFolder = GetFiles(yourDir);
if(FilesInFolder!=null) {
customAdapter.updateData(FilesInFolder);
}
}
文件sdCardRoot=Environment.getExternalStorageDirectory();
yourDir=新文件(sdCardRoot,“/MoreData/closed”);
fileinfolder=GetFiles(yourDir);
if(fileinfolder!=null){
customAdapter=新的customAdapter(getActivity(),fileInfolder);
lv.setAdapter(自定义适配器);
customAdapter.notifyDataSetChanged();
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
最终文件=新文件(path.get(position));
value_new=fileinfolder.get(position.toString();
filefrom=新文件(root,String.valueOf(File.getName());
File to=新文件(root,String.valueOf(File.getName()).substring(0,File.getName().lastIndexOf(“)”)+1)+newencrypt);
由.更名为(至);;
文件sdCardRoot=Environment.getExternalStorageDirectory();
yourDir=新文件(sdCardRoot,“/MoreData/closed”);
fileinfolder=GetFiles(yourDir);
if(fileinfolder!=null){
updateData(fileinfolder);
}
}
重命名文件后需要调用notifyDataSetChanged()方法。尝试了!!!但没有成功:(请检查renameTo()函数的返回值。如果是真的,则只有重命名成功。itz successfull!!!我在文件管理器上通过放置notifyDataSetChanged()完成了检查)+通过@Gordak!!更新数据,但不刷新片段!!我将列表放入片段!!!!每当你想刷新listview时。例如,一旦文件被重命名。只需调用customAdapter.updateData(UpdateList)。我使用此==文件from=新文件(root,String.valueOf(file.getName());文件to=新文件(root,String.valueOf(file.getName()).substring(0,file.getName().lastIndexOf(“)”+1)+newencrypt);from.renameTo(to);customAdapter.updateData(fileInfolder);但不刷新列表井…首先如何构建适配器?查看在单击该方法之前编写的内容。在onClick方法中