【问题标题】:Android java : How to auto refresh list viewAndroid java:如何自动刷新列表视图
【发布时间】:2014-03-16 16:29:06
【问题描述】:

我目前正在开发一个文件资源管理器应用程序,我需要为我的列表视图自动刷新。 因为当我删除该项目时,我仍然可以看到该项目,我需要退回文件夹并再次进入该文件夹只会看到它消失。如何自动刷新,所以当我删除项目时它会自行消失。

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    super.onListItemClick(l, v, position, id);
    FileInfo fileDescriptor = fileArrayListAdapter.getItem(position);
    if (fileDescriptor.isFolder() || fileDescriptor.isParent()) {
        currentFolder = new File(fileDescriptor.getPath());
        fill(currentFolder);
        //


    } else {

        fileSelected = new File(fileDescriptor.getPath());
        Intent intent = new Intent();
        intent.putExtra(Constants.KEY_FILE_SELECTED,
                fileSelected.getAbsolutePath());
        setResult(Activity.RESULT_OK, intent);
        Log.i("FILE CHOOSER", "result ok");



            MimeTypeMap map = MimeTypeMap.getSingleton();
            String ext = MimeTypeMap.getFileExtensionFromUrl(fileSelected.getName());
            String type = map.getMimeTypeFromExtension(ext);

            if (type == null){
                type = "*/.jpeg*";

            intent.setAction(android.content.Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(fileSelected), "image/*");

          //  intent.setDataAndType(data, type);
            }else {
                 type = "*/.txt*";

                    intent.setAction(android.content.Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(fileSelected), "text/*");

                 //   intent.setDataAndType(data, type);
            }
        //finish();


    //  intent.setAction(android.content.Intent.ACTION_VIEW);

        startActivity(intent); 
    //  Intent intent = new Intent();




        this.getListView().setLongClickable(true);
        this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
        new AlertDialog.Builder(ViewNoteActivity.this) .setTitle("Delete File")
        .setMessage("Do You Want To Delete this file ?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int button){
                fileSelected.delete();
                Toast.makeText(getBaseContext(), "File has been deleted." , Toast.LENGTH_SHORT ).show();
                adapter.notifyDataSetChanged();
            }

        })
        .setNegativeButton("No", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int button){
            }
        }).show();
         return true;
            }
        });

    }
}



public static boolean deleteDir(File dir) {
    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i=0; i<children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }
    return dir.delete();

}

【问题讨论】:

    标签: java android android-listview refresh


    【解决方案1】:

    从填充列表视图的数据源中删除项目并在适配器上调用notifyDataSetChanged 以刷新列表视图

    【讨论】:

    • 您好,感谢您的回答。我是 android 新手,你能给notifyDataSetChanged 举一些例子或更多细节吗?我不知道必须放在哪里
    • @Frozen 使用adapter.notifyDataSetChanged()
    • @Frozen 您需要向我们展示您的代码。适配器未解析意味着它的变量未声明。向我们展示您的代码以更好地帮助您。
    • @Frozen 适配器在哪里声明了初始化?你也有fileSelected.delete();,你有deleteDir(File dir)
    【解决方案2】:

    您需要从 Adapter 的 ArrayList 中删除 ListItem 并调用 notifyDatasetChanged() , 当您调用 notifyDatasetChanged() 方法时,它将使用带有更新数据的 ArrayList 刷新您的列表视图,因此,您必须首先从 ArrayList onClick of Delete Button 中删除项目。 然后你必须调用 notifyDatasetChanged()。

    【讨论】:

      猜你喜欢
      • 2014-12-14
      • 1970-01-01
      • 2011-08-31
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      • 2018-07-27
      • 2011-10-23
      • 1970-01-01
      相关资源
      最近更新 更多