package net.samaysoftware.sampleapplication12345;
import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
public class FMMainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
ListView lv;
String[] from = {"fname","ftype"};
int[] to = {android.R.id.text1, android.R.id.text2};
ArrayList<HashMap<String, String>> data = new ArrayList<>();
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fmmain);
lv = (ListView) findViewById(R.id.fmlv);
generateData();
SimpleAdapter sa = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2,from, to);
lv.setAdapter(sa);
lv.setOnItemClickListener(this);
}
private void generateData() {
File froot = null;
if(getIntent().getStringExtra("abcd")==null) {
froot = Environment.getExternalStorageDirectory();
}
else{
String fpath = getIntent().getStringExtra("abcd");
froot = new File(fpath);
setTitle(froot.getName());
}
File[] arr = froot.listFiles();
for(File f: arr){
String name = f.getName();
String type = f.isFile()?"File":"Folder";
HashMap<String, String> map = new HashMap<>();
map.put("fname", name);
map.put("ftype", type);
map.put("fpath", f.getAbsolutePath());
data.add(map);
}
}
@Override public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
String fpath = data.get(pos).get("fpath");
File cf = new File(fpath);
if(cf.isDirectory()){
Intent i = new Intent(this, FMMainActivity.class);
i.putExtra("abcd", fpath);
startActivity(i);
}else{
}
}
}
Note: Add permission in Manifest file as below:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
just before the <application> tag and inside the <manifest> tag.
No comments:
Post a Comment