Android 如何在ListView中显示文件的arraylist
我使用此函数在Android 如何在ListView中显示文件的arraylist,android,listview,android-listview,arraylist,android-sdcard,Android,Listview,Android Listview,Arraylist,Android Sdcard,我使用此函数在文本视图中显示ArrayList,它将显示SD卡中的所有目录和文件,但我想改用ListView。如何在ListView中显示SD卡的目录和所有文件 方法getfile读取所有文件和目录,并将其添加到ArrayList类对象作为文件列表: private File root; private ArrayList<File> fileList = new ArrayList<File>(); private LinearLayout view; private
文本视图中显示ArrayList
,它将显示SD卡中的所有目录和文件,但我想改用ListView
。如何在ListView
中显示SD卡的目录和所有文件
方法getfile
读取所有文件和目录,并将其添加到ArrayList
类对象作为文件列表:
private File root;
private ArrayList<File> fileList = new ArrayList<File>();
private LinearLayout view;
private EditText filename;
private Button search;
private TextView allfiles,searchfile;
Spannable txt;
boolean check=false;
ListView l;
String [] strarray;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l=(ListView)findViewById(R.id.listView1);
view = (LinearLayout) findViewById(R.id.view);``
filename=(EditText)findViewById(R.id.filename);
allfiles=(TextView)findViewById(R.id.allfiles);
searchfile=(TextView)findViewById(R.id.searchfile);
search=(Button)findViewById(R.id.search);
String path=Environment.getExternalStorageDirectory().toString();
root = new File(path);
getfile(root);
for (int i = 0; i < fileList.size(); i++)
{
if (fileList.get(i).isDirectory())
{
txt=new SpannableString(fileList.get(i).getName());
txt.setSpan(new ForegroundColorSpan(Color.RED), 0, txt.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
allfiles.append(txt);
allfiles.append("\n");
}
// if file than show in default color
else
{
allfiles.append(fileList.get(i).getName());
allfiles.append("\n");
allfiles.setPadding(5, 5, 5, 5);
}
}
首先了解及其关联的,然后参考以下线程来回答您的问题
此外,为了在listview中搜索项目,您需要执行以下步骤:
将TextWatcher添加到EditText李>
在listview BaseAdapter中实现接口
有关更多信息,请参阅。希望这能帮助您开始:)
public ArrayList<File> getfile(File dir)
{
File listFile[] = dir.listFiles();
if (listFile != null && listFile.length > 0)
{
for (int i = 0; i < listFile.length; i++)
{
if (listFile[i].isDirectory())
{
fileList.add(listFile[i]);
getfile(listFile[i]);// recursive calling to scan hole sdcard
}
else
{
//condition to check file exists or not if yes add it to searchfile text view written below
if(listFile[i].getName().toString().substring(0,listFile[i].getName().toString().indexOf(".")).compareToIgnoreCase(filename.getText().toString())==0)
{
searchfile.append(listFile[i].getName());
searchfile.append("\n");
check=true;
}
fileList.add(listFile[i]);
}
}
return fileList;