Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.os.Environment;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.*;
- import android.widget.AdapterView.OnItemClickListener;
- import java.io.File;
- public class MainLayoutActivity extends AppCompatActivity
- {
- ListView listView;
- File currentTargetDir;
- int level=0;
- class Listener implements OnItemClickListener
- {
- @Override
- public void onItemClick(AdapterView<?> parent,View view,int position,long id)
- {
- File selected= new File(currentTargetDir.getAbsolutePath() +"/"+ ( (TextView)view).getText().toString() );
- if(selected.exists() && selected.isDirectory() )
- {
- currentTargetDir=selected;
- initAdapter();
- level++;
- }
- }
- }
- void initAdapter()
- {
- String fileList[]= currentTargetDir.list();
- ArrayAdapter<String> adapter= new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,fileList);
- listView.setAdapter(adapter);
- }
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main_layout);
- listView= (ListView)findViewById(R.id.list);
- listView.setOnItemClickListener(new Listener());
- if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
- {
- currentTargetDir= new File(Environment.getExternalStorageDirectory().getAbsolutePath() );
- initAdapter();
- }
- else
- Toast.makeText(this,"Карта не найдена",Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onBackPressed()
- {
- if(level==0)
- finish();
- else
- {
- level--;
- currentTargetDir= currentTargetDir.getParentFile();
- initAdapter();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement