2016-11-11 31 views
2

我通過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()); 
      } 
     } 
    } 
+0

你需要調用notifyDataSetChanged()方法重命名文件之後。 –

+0

試過!!!但沒有運氣:( –

+0

請檢查renameTo()函數的返回值。如果它是true,那麼只有重命名成功。 –

回答

1

調用notifyDataSetChanged()的的onclick

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

不刷新列表兄弟!!!!我試過!! :( –

0

customAdapter.notifyDataSetChanged()裏面應該也叫你onItemClick()結束

0

notifyDataSetChanged只會重新繪製listview,它使用適配器知道要繪製什麼。您的適配器使用ArrayList作爲數據輸入。如果在調用notifyDataSetChanged之前未更改此對象,則ListView將保持完全相同。

要反映列表中的更改,必須更改附加到適配器的數據。所以你應該提供一種方法來允許修改你的適配器數據。

例如,這種方法添加到您的CustomAdapter:

public void updateData(ArrayList<String> countryList) { 
    this.countryList = countryList; 
    notifyDataSetChanged(); 
} 

然後單擊某個項目時,相應地調整文件列表,更新您的適配器。

UPDATE:

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

     } 
+0

我在哪裏使用updateData方法在活動中? –

+0

每當你想刷新你的列表視圖,例如一旦文件被重命名,只需調用customAdapter.updateData(updatedList)。 – Gordak

+0

我使用這個== File = from 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 ); customAdapter.updateData(FilesInFolder);但不刷新列表 –

相關問題