使用基本适配器重命名文件后刷新列表视图

时间:2016-11-11 07:51:46

标签: android listview baseadapter

我通过arraylist在listview中获取我的文件夹文件。 @ on点击我重命名文件但我的listview没有使用adapter.datasetnotifychanged刷新。我正在使用自定义基础适配器。可以告诉我如何在"从"重命名后自动刷新列表到"重命名为"文件。

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());
            }
        }
    }

3 个答案:

答案 0 :(得分:1)

在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();
  }

答案 1 :(得分:0)

最后还应在你的onItemClick()中调用customAdapter.notifyDataSetChanged()

答案 2 :(得分:0)

notifyDataSetChanged只会重绘listview,后者使用适配器知道要绘制的内容。您的适配器使用ArrayList作为数据输入。如果在调用notifyDataSetChanged之前未更改此对象,则ListView将保持完全相同。

要反映列表中的更改,您必须更改附加到适配器的数据。因此,您应该提供一种允许修改适配器数据的方法。

例如,将此方法添加到CustomAdapter:

public void updateData(ArrayList<String> 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);
           }

      }