有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java区分文件和目录

我正在用安卓做一个文件浏览器。所以在列表视图中,我显示了所有的文件夹和文件,但目前都有相同的图标,即一个文件夹。我想要文件夹和目录的不同图标。此外,当我点击一个文件时,我想看到可以打开它的应用程序列表。目前,我可以打开一个目录时,点击一个文件,我显示一个土司无法打开。 已经想了很多事情,但仍然无法理解 感谢您的帮助。提前谢谢。 以下是我的活动:

package com.rrawat.fileexplorer;

import 安卓.app.ActionBar;
import 安卓.app.ListActivity;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.ArrayAdapter;
import 安卓.widget.ListView;
import 安卓.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class ListFileActivity extends ListActivity {

    private String path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_files);

        // Use the current directory as title
        path = "/sdcard";
        if (getIntent().hasExtra("path")) {
            path = getIntent().getStringExtra("path");
        }
        setTitle(path);

        // Read all files sorted into the values-array
        List values = new ArrayList();
        File dir = new File(path);
        if (!dir.canRead()) {
            setTitle(getTitle() + " (inaccessible)");
        }
        String[] list = dir.list();
        if (list != null) {
            for (String file : list) {
                if (!file.startsWith(".")) {
                    values.add(file);
                }
            }
        }
        Collections.sort(values);

        // Put the data into the list
        this.setListAdapter(new ArrayAdapter<String>(this,R.layout.mylist,R.id.Itemname,values));
        /*ArrayAdapter adapter = new ArrayAdapter(this,
                安卓.R.layout.simple_list_item_2, 安卓.R.id.text1, values);
        setListAdapter(adapter);*/

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        String filename = (String) getListAdapter().getItem(position);
        if (path.endsWith(File.separator)) {
            filename = path + filename;
        } else {
            filename = path + File.separator + filename;
        }
        if (new File(filename).isDirectory()) {
            Intent intent = new Intent(this, ListFileActivity.class);
            intent.putExtra("path", filename);
            startActivity(intent);
        } else {
            Toast.makeText(this, filename + " is not a directory", Toast.LENGTH_LONG).show();
        }
    }
}

我的XML:

<LinearLayout
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent">

    <ListView

        安卓:id="@安卓:id/list"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content" />
</LinearLayout>

我的名单。xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="fill_parent"
    安卓:layout_height="fill_parent"
    安卓:orientation="horizontal" >
    <ImageView
        安卓:id="@+id/icon"
        安卓:layout_width="50dp"
        安卓:layout_height="50dp"
        安卓:layout_marginBottom="5dp"
        安卓:layout_marginLeft="5dp"
        安卓:layout_marginRight="5dp"
        安卓:layout_marginTop="5dp"
        安卓:src="@drawable/folder" />
    <TextView
        安卓:id="@+id/Itemname"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:textSize="20sp"
        安卓:paddingTop="5dp"/>
</LinearLayout>

共 (2) 个答案

  1. # 1 楼答案

    正如Bojan Kseneman所说,您必须创建一个自定义适配器,然后覆盖getView()

    现在,您正在使用ArrayAdapter的默认实现,但是您可以创建一个extends ArrayAdapter的类,并将其定义为:

    public class FilesAndFoldersAdapter extends ArrayAdapter<String> {
        public FilesAndFoldersAdapter(Context context, ArrayList<String> values) {
           super(context, 0, values);
        } 
    
        @Override 
        public View getView(int position, View convertView, ViewGroup parent) {
           // Get the data item for this position 
           String filePath = getItem(position);    
           // Check if an existing view is being reused, otherwise inflate the view 
           if (convertView == null) {
              convertView = LayoutInflater.from(getContext()).inflate(R.layout.mylist, parent, false);
           } 
    
           // Lookup view for data population 
           TextView tvName = (TextView) convertView.findViewById(R.id.Itemname);
           ImageView ivImage = (ImageView) convertView.findViewById(R.id.icon);
    
           // Populate the data into the template view using the data object 
           tvName.setText(filePath);
    
           if (new File(filePath).isDirectory()) {
               ivImage.setImageResouce(R.drawable.folder_icon);
           } else {
               ivImage.setImageResouce(R.drawable.file_icon);
           }
    
           // Return the completed view to render on screen 
           return convertView;
       } 
    } 
    

    这段代码的重要部分是检查文件是目录还是文件,然后使用ImageView上的相应图标

    创建自定义适配器类后,只需将其设置为ListView的适配器,如下所示:

    setListAdapter(new FilesAndFoldersAdapter(this, values));
    
  2. # 2 楼答案

    您可以使用它来确定文件是否为目录

    boolean isDir = new File(path).isDirectory();