我有項目,我不知道如何獲取文件夾中的所有文件名。我在Andoid Studio工作。我只需要使函數getAllFilesInFolder(string folderPath)。如果您編寫所需的所有進口,我也會很高興。代碼我使用:如何獲取Java文件夾中的所有文件
package com.my.appPackage;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] files = getAllFilesInFolder("/sdcard/folderIUse");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.LargeTextInList, files);
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
list.setOnItemClickListener(new ItemList());
}
class ItemList implements AdapterView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
ViewGroup vg = (ViewGroup) view;
TextView tv = (TextView) vg.findViewById(R.id.LargeTextInList);
Toast.makeText(MainActivity.this, tv.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
}