Monday, December 7, 2015

ListView for Displaying File

From the last documentation about  ListView I have explored on how to make a list of string. I though that clicking on a list view will certainly play a sound file (for the time being *mp3 files). But first thing first is about clicking a directory name that will show what kinds of files reside inside it.

The program requires several objects  that can accommodate  above action. The classes are :
  1. FileArray.
  2. File
The steps of operation on how to operate on files are listed below : 
  1. Environment.getExternalStorageDirectory.listFiles() returns directories and files inside a particular directory. The returns (plural : because it returns many files object) are stored inside file array.  
  2. An object of ArrayList will hold array of file (representation of file or directory)
  3. An object of ArrayAdapter that acts as adapter between object of ArrayList and object of ListView
To make things short I show layout file :
/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    Copyright 2015, Joko Adianto 

   */
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

Next is MainActivity.java file (this is not a complete program, It cannot play music when i click a file , not a directory, it gives an error. This is not a complete program. It will be complete on the next article when it can play music) :

/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    Copyright 2015, Joko Adianto 

   */
package com.example.joko.listviewfiles;

import android.media.MediaPlayer;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ListView listView;
    ArrayAdapter<String> arrayAdapter;
    File fileArray[];
    MediaPlayer mediaPlayer; //Declare Reference Variable

    @Override    protected void onDestroy() {
        mediaPlayer.stop();
        super.onDestroy();
    }

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        int i;
        int j;

        fileArray = Environment.getExternalStorageDirectory().listFiles();
        listView = (ListView) findViewById(R.id.listView);
        mediaPlayer = new MediaPlayer(); //mediaPlayer refers a new object of MediaPlayer;        final ArrayList<String> arrayList = new ArrayList<String>();
        j = fileArray.length;
        for(i=0; i< j; i++){
            arrayList.add(fileArray[i].getPath()+"");
        }

        arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, arrayList);

        // Assign adapter to ListView        listView.setAdapter(arrayAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int i, j;
                String str = (String) parent.getItemAtPosition(position);
                arrayList.clear();
                File file = new File(str);
                arrayList.clear();
                arrayList.add(str);
                listView.setAdapter(arrayAdapter);

                fileArray = file.listFiles();
                j = fileArray.length;
                for (i = 0; i < j; i++) {
                    arrayList.add(fileArray[i].getPath() + "");
                    }
                    listView.setAdapter(arrayAdapter);
                }

        });
    }
}
 
Next I am going to play *mp3 files.

No comments:

Post a Comment