Tuesday 22 March 2016

Andoird Developer Notes--A very small file chooser(incomlete)

I need to developer a small and efficient file chooser specifying  format '.txt' in a little app.
At the beginning, I tried to use an external lib module from github https://github.com/iPaulPro/aFileChooser , but I don't know how to add an external module in android studio, and I read so many documents I have new a project to test this module, but it doesn't work. So I need to learn how the android studio build a project and how the Manifest works.

Then I try to realize this function by myself.
1.I use ListActivity class, define the ViewContent
2.Get the file path by android.os.Environment.getExternalStorageDirectory
3.Detext the file path list if it's a txt file or a directory or other file, choose the true file put into Arraylist
4.SetAdapter into the list given by ListActivity.
5.Get the file path which is choosen, and get the content of file
6.New a bufferStream to store file content and prepare to be handled by other activity.

Disadvantages:  No method to return last directory if you want to back. 

there is the code:
public class FileList extends ListActivity {
    private List fileNameList;
    private String filename;
    private Controller ctr = null;
    @Override
    protected void onCreate(Bundle savedInstenceState){
        super.onCreate(savedInstenceState);
        setContentView(R.layout.filelist);
        ctr = new Controller(this);
        initFileList();
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        File file = fileNameList.get(position);
        if (file.isDirectory())
        {
            File[] f = file.listFiles();
            fill(f);
        }
        else {
            //get the absolute path of file
            filename = file.getAbsolutePath();
            getFileString(filename);// here I got the content of the file
            Intent i = new Intent(FileList.this, ActivityList.class);
            startActivity(i);
        }
    }

    private void getFileString(String filename) {
        try{
            FileInputStream fileInputStream = new FileInputStream(filename);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String n;
            Toast.makeText(getApplicationContext(), "file chooser", Toast.LENGTH_LONG).show();
            while ((n = bufferedReader.readLine()) != null){
                String [] word_list = n.split(" ");
                getContent(word_list);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }


    private void initFileList() {
        File file = android.os.Environment.getExternalStorageDirectory();
        Toast.makeText(getApplicationContext(), "file chooser start", Toast.LENGTH_SHORT).show();
        File[] f  = file.listFiles();
        fill(f);
    }

    private void fill(File[] files) {
        fileNameList = new ArrayList();
        for (File file : files){
            if(isValidFileOrDirName(file)){
                fileNameList.add(file);
            }
        }
        ArrayAdapter adapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, fileToStrArr(fileNameList));
        setListAdapter(adapter);
    }


    //con
    private String[] fileToStrArr(List fileNameList) {
        ArrayList fileList = new ArrayList();
        for(int i = 0; i < fileNameList.size(); ++i){
            fileList.add(fileNameList.get(i).getName());
        }
        return fileList.toArray(new String[0]);
    }


    //Check if the file name is valid like txt
    private boolean isValidFileOrDirName(File file){
        boolean bool = false;
        Toast.makeText(getApplicationContext(), "file chooser detect valid name", Toast.LENGTH_SHORT).show();
        if(file.isDirectory()){
            bool = true;
        } else {
            String fileName = file.getName().toLowerCase();
            if(fileName.endsWith(".txt")){
                bool = true;
            }
        }
        return bool;
    }


    private void getContent(String[] word_list){
        Words word = new Words();
        List list = Arrays.asList(word_list);
        if(word != null){
            for (int i = 0; i < list.size(); i++) {
                word.setWord(list.get(i).toString());
                word.setMeaning("Search it by yourself, app have not yet dictionary.");
                word.setLevel(3);

                Toast.makeText(getApplicationContext(), "getContent", Toast.LENGTH_LONG).show();
                try {
                    boolean bool = ctr.save(word);
                    if (bool == false) {
                        Toast.makeText(this, "Word: " + word.getWord() + "can't be pushed into DB",
                                Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else {
            Toast.makeText(getApplicationContext(), "word is null", Toast.LENGTH_LONG).show();
        }
    }
}


Note: I am a green hand,  just after his small program I have knew the different between Activity and ListActivity,
In fact, there is few difference between this two class when we use listView,  ListActivity did some optimisation in showing listView.
     
How to use listView in Activity?
1.At the layout XML, we can use any name to listview id
2.The class which extends Activity use findViewById to add listView object.
3.Using listView.setAdapter(xxx) to bundle data in the list.

How to use listView in ListActivity?
1. At the layout XML, we must use "@+id/android:list" for its id.
2.We use directly setListAdapter(xxx) to bundle data.
3. If we want to get ListView item, use ListView lv = getListView() method.

No comments:

Post a Comment