[英]differentiate between files and directories
我正在用android做一個文件瀏覽器。 因此,在列表視圖中,即時消息顯示了所有文件夾和文件,但當前所有圖標具有相同的圖標,即文件夾的圖標。 我想要文件夾和目錄的不同圖標。 此外,當我單擊一個文件時,我想查看可以打開它的應用程序列表。 當前無法在單擊時打開目錄,在單擊文件時無法打開烤面包。 已經考慮了很多事情,但仍無法弄清任何幫助。 提前致謝。 這是我的活動:
package com.rrawat.fileexplorer;
import android.app.ActionBar;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.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,
android.R.layout.simple_list_item_2, android.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:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
mylist.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="@drawable/folder" />
<TextView
android:id="@+id/Itemname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:paddingTop="5dp"/>
</LinearLayout>
您可以使用它來確定文件是否為目錄。
boolean isDir = new File(path).isDirectory();
您必須創建一個自定義適配器,然后如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));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.