csv parse

Android CSV Reader

Android CSV Reader

In some android application, comma-separated values (CSV) is also called character separated values need to parse and retrieve data from csv. In this tutorial we are going to learn how read csv file from assets file and show data to list view in android. place student.csv in assets folder.

 

Now check Source Code

 

CSVActivity.java


 

package com.androidfromhome.csvreader;

import java.io.IOException;
import java.io.InputStreamReader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import com.androidfromhome.adapter.AndroidListAdapter;
import com.androidfromhome.adapter.StudentCollection;

public class CSVActivity extends Activity {

    private AndroidListAdapter adapter;
    private ListView lv_csv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_csv);
        
        lv_csv=(ListView)findViewById(R.id.lv_csv);
        
        try {
            InputStreamReader input_stream=    new InputStreamReader(getAssets().open("student.csv"));
             CSVReader csv_reader = new CSVReader(input_stream);
             csv_reader.getData();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
         adapter=    new AndroidListAdapter(CSVActivity.this,R.layout.list_item,StudentCollection.studentCollectionArr);
         lv_csv.setAdapter(adapter);
    
    }

    
}


 

 

CSVReader.java


 

package com.androidfromhome.csvreader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import com.androidfromhome.adapter.StudentCollection;

public class CSVReader {
    InputStreamReader inputStream;

    public CSVReader(InputStreamReader insr){
        this.inputStream = insr;
    }

    public void getData(){
        StudentCollection.studentCollectionArr=new ArrayList<StudentCollection>();
        BufferedReader reader = new BufferedReader(inputStream);
        try {
            String csv_item;
            while ((csv_item = reader.readLine()) != null) {
                String[] row = csv_item.split(",");
               String name= row[0];
               String roll= row[1];
               String sub= row[2];
               StudentCollection.studentCollectionArr.add(new StudentCollection(name, roll, sub));
            }
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
        finally {
            try {
                inputStream.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        
    }
}


 

 

AndroidListAdapter .java


 

package com.androidfromhome.adapter;

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.androidfromhome.csvreader.R;

public class AndroidListAdapter extends ArrayAdapter<StudentCollection>{
    
    private final Context context;
    private final ArrayList<StudentCollection> values;
    private ViewHolder viewHolder;
    private final int resourceId;

    public AndroidListAdapter(Context context, int resourceId,ArrayList<StudentCollection> values) {
        super(context, resourceId, values);
        // TODO Auto-generated constructor stub
        
        this.context = context;
        this.values = values;
        this.resourceId = resourceId;
    }
    
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(resourceId, parent, false);
            
                
            viewHolder = new ViewHolder();
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_roll = (TextView) convertView.findViewById(R.id.tv_roll);
            viewHolder.tv_sub = (TextView) convertView.findViewById(R.id.tv_sub);
            
            
                convertView.setTag(viewHolder);
            
            
        }else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        StudentCollection student_obj=values.get(position);
        viewHolder.tv_name.setText("Name: "+student_obj.name);
        viewHolder.tv_roll.setText("Roll No: "+student_obj.roll);
        viewHolder.tv_sub.setText("Subject: "+student_obj.sub);
            
        return convertView;
    }
   
    
    public class ViewHolder {

         TextView tv_name;
         TextView tv_roll;
         TextView tv_sub;

    }

}


 

StudentCollection.java


 

package com.androidfromhome.adapter;

import java.util.ArrayList;

public class StudentCollection {

    public static ArrayList<StudentCollection> studentCollectionArr=null;

    public String name="";
    public String roll="";
    public String sub="";
    public StudentCollection(String name,String roll,String sub){
        this.name=name;
        this.roll=roll;
        this.sub=sub;
        
    }
    
}

 


 

 

Now xml code

activity_csv.xml


 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.androidfromhome.csvreader.CSVActivity" >

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#A758AB"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="15dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:text="Android From Home"
            android:textColor="#ffffff"
            android:textSize="22dp"
            android:textStyle="bold" />
    </LinearLayout>

    <ListView
        android:id="@+id/lv_csv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

 


 

list_item.xml


 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="#ffffff"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:gravity="center"
        android:padding="6dp" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tv_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:text="TextView"
                android:textColor="#000000"
                android:textSize="16dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tv_roll"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:text="TextView"
                android:textColor="#343434"
                android:textSize="14dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tv_sub"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:maxLength="40"
                android:padding="2dp"
                android:text="Some Text Goes here.."
                android:textColor="#343434"
                android:textSize="12dp"
                android:textStyle="bold" />

        </LinearLayout>

    </LinearLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#dfdfdf" />

</LinearLayout>


 

Related Post:-

Android custom calendar

Font Style For Android

Sqlite database example in android

ListView With section header in android example

Leave a Reply

Your email address will not be published. Required fields are marked *

Get Widget