THIS CONTENT DOWNLOAD SHORTLY

Objective

Main objective of this tutorial is to explain how to parse the CSV file in Android.

 

You will get Final Output:

  • csv-selected-categroy

We are using Category.csv file in this example - you can follow the steps given below.

Screenshot of CSV file:

csv-file

 

Step 1 Add .jar files

First we need to add following Jar file

  • opencsv-2.3.jar

You can download this jar file from the following link:

 

Step 2 CSVReader Class

Assume that Our CSV file contains two fields:

Fields Datatype
categoryId Integer
categoryName String

We can fetch these fields data using the following code:

String next[] = {};
List<string[]> list = new ArrayList<string[]>();
try {
    CSVReader reader = new CSVReader(new InputStreamReader(getAssets().open("test.csv")));//Specify asset file name
    //in open();
        for(;;) {
            next = reader.readNext();
            if(next != null) {
                list.add(next);
            } else {
                break;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

Now If we want to fetch categoryId and categoryName fields than we can use the list as following:

list.get(row index)[column index]

i.e:

list.get(1)[0] This will return categoryId of Integer from first row of csv file.
list.get(1)[1] This will return categoryName of String from first row of csv file.

 

Note

list.get(index): This will return array that contains data of particular row of csv file.

private ArrayList categoryList = new ArrayList();
    for (int i = 0; i < list.size(); i++) {
        categoryList.add(list.get(i)[1]);
    }

I hope you found this blog helpful Parse CSV file in Android. Let me know if you have any questions or concerns regarding  Parsing CSV file in Android, please put a comment here and we will get back to you ASAP.

Got an Idea of Android App Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Android App Development Company in India.

An entrepreneur who has founded 2 flourishing software firms in 7 years, Tejas is keen to understand everything about gaming - from the business dynamics to awesome designs to gamer psychology. As the founder-CEO of a company that has released some very successful games, he knows a thing or two about gaming. He shares his knowledge through blogs and talks that he gets invited to.

face mask Belial The Demon Headgear Pocket Staff Magic