下面是实现读取SD卡下所有TXT文件名并用listView显示出来的方法的攻略:
- 确认权限
首先我们需要在AndroidManifest.xml中添加读取SD卡权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- 获取SD卡目录
获取SD卡的根目录路径:
String sdCardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
- 遍历TXT文件
遍历SD卡下所有的TXT文件,获取文件名并添加到List中:
File sdCardFolder = new File(sdCardPath);
List<String> txtFileList = new ArrayList<>();
if (sdCardFolder.exists() && sdCardFolder.isDirectory()) {
File[] files = sdCardFolder.listFiles();
for (File file : files) {
if (file.isFile() && file.getName().endsWith(".txt")) {
txtFileList.add(file.getName());
}
}
}
- 绑定ListView
将txtFileList绑定到ListView上:
ListView listView = findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, txtFileList);
listView.setAdapter(adapter);
下面是完整的代码示例:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String sdCardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
File sdCardFolder = new File(sdCardPath);
List<String> txtFileList = new ArrayList<>();
if (sdCardFolder.exists() && sdCardFolder.isDirectory()) {
File[] files = sdCardFolder.listFiles();
for (File file : files) {
if (file.isFile() && file.getName().endsWith(".txt")) {
txtFileList.add(file.getName());
}
}
}
ListView listView = findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, txtFileList);
listView.setAdapter(adapter);
}
}
另外,可以参考以下两个示例:
- 按钮触发读取:
public class MainActivity extends AppCompatActivity {
private List<String> txtFileList = new ArrayList<>();
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, txtFileList);
ListView listView = findViewById(R.id.listView);
listView.setAdapter(adapter);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
readTxtFile();
}
});
}
private void readTxtFile() {
String sdCardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
File sdCardFolder = new File(sdCardPath);
if (sdCardFolder.exists() && sdCardFolder.isDirectory()) {
File[] files = sdCardFolder.listFiles();
txtFileList.clear();
for (File file : files) {
if (file.isFile() && file.getName().endsWith(".txt")) {
txtFileList.add(file.getName());
}
}
adapter.notifyDataSetChanged();
}
}
}
- 添加文件长按事件:
public class MainActivity extends AppCompatActivity {
private List<String> txtFileList = new ArrayList<>();
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, txtFileList);
ListView listView = findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
String fileName = txtFileList.get(position);
deleteFileDialog(fileName);
return true;
}
});
readTxtFile();
}
private void readTxtFile() {
String sdCardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
File sdCardFolder = new File(sdCardPath);
if (sdCardFolder.exists() && sdCardFolder.isDirectory()) {
File[] files = sdCardFolder.listFiles();
txtFileList.clear();
for (File file : files) {
if (file.isFile() && file.getName().endsWith(".txt")) {
txtFileList.add(file.getName());
}
}
adapter.notifyDataSetChanged();
}
}
private void deleteFileDialog(final String fileName) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("删除文件");
builder.setMessage("是否删除:" + fileName);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String sdCardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
File file = new File(sdCardPath, fileName);
if (file.exists()) {
file.delete();
readTxtFile();
}
}
});
builder.setNegativeButton("取消", null);
builder.show();
}
}
希望能够对你有帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android实现读取SD卡下所有TXT文件名并用listView显示出来的方法 - Python技术站