CSV Writer in Android (Email that CSV)

CSV Write in 15 mins in Android:
1. First and most important thing. I am using SuperCSV lib. Here http://supercsv.sourceforge.net/ the link if you want to learn complete power of this lib. I recommend this link. But if you have no time because you want to complete your work in 15mins and send to client then yes you read my tutorial but i again recommend that link.

1. Create Android project and download libs from above link and buildpath in Android project.

2. Create a little form.


3. Add permissions in Android manifest.

Here Internet permission require if you want to email this CSV.

4. Now final round we are going to write Java code. Yes great.
Here i only describe the CSV related things not Android basic programming :) .
So there 4 important steps.
1. ANSWER object on line 36:
                              In this object we save our data which we want to write on CSV  file.
2. isAlreadyExist on line 37:
                             Not important only i use to check if file already exist then don't override the headers of        
    CSV.
3. desFile on line 38:
                           This is the CSV file where we save our data.
4. CSV_HEADER on line 40:
              Here we have String array with Header names. Now here you add header of CSV according to your requirement. For your easiness please see below picture.

private static final String CSV_HEADER[] = { "FirstName", "LastName", "Emmail" };



5. I assume you get the data from user and now user click on Submit button.
Here three functions call.
1 addData() here i add the data in ANSWER object.


2. sdCardHandler() here i handle ever thing which related with SD Card.
 I think it is very easy not require any description.
3. Writing CSV:
Again i think it is very easy. But here i am describing.
First i get an ICsvMapWrite object wit name mapWriter.
After that i nitialize this mapWriter object.
Now next, oh here is little tricky thing. but not tough. Like you see getProcessors() method.
This method give me the constraints which i am applying on each column. just like in MySql or any other database we apply on some columns Not Null, On some columns we apply primary constraint. Same here we apply, only thing is here we apply constraint on every column. Where you feel not require any constraint give Optional(). Oh sorry  now you read all the above text OK no problem now see the below picture.


Complete.
Trust me. We complete our assignment. If you face any problem you can download code from link.
https://github.com/Hafiz-Waleed-Hussain/CSV-Writer-For-Android

One more bonus thing.
Now send email that CSV.
OK Simple.

OK bye.

3 comments:

  1. Very usefull tutorial... tahnks @Hafiz Waleed Hussain
    you really made my day
    !

    ReplyDelete
  2. This was super useful to me, thanks! The only thing I needed to change was when I initialised the Hashmap, I had to do the following in Android:

    HashMap ANSWER = new HashMap<>();

    Otherwise I would get a null pointer.

    Thanks!

    ReplyDelete