3

I made a simple application that will log a user input from an editText and it will put it into a csv on my sdcard. Now my problem is reading that csv and making it as a variable for the next activity on my app.

Here is my main activity:

package com.willardcuizon.differentialgps;

import java.io.File;

import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Main extends Activity implements OnClickListener{

Button start;
EditText lat;
EditText lon;

LocationListener loclist;
CSVFileWriter csv;
StringBuffer filePath;
File file;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    start = (Button) findViewById(R.id.button1);
    start.setOnClickListener(this);

    lat = (EditText) findViewById(R.id.editText1);
    lon = (EditText) findViewById(R.id.editText2);


    filePath = new StringBuffer();
    filePath.append("/sdcard/surveyedGPS.csv");
    file = new File(filePath.toString());

    csv = new CSVFileWriter(file);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v.getId()==R.id.button1){
        LocationManager locman =        (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        if(locman.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            Toast.makeText( getApplicationContext(), "GPS is Enabled.",  Toast.LENGTH_SHORT ).show();
            csv.writeHeader(lat.getText().toString());
            csv.writeHeader(lon.getText().toString());

        }
        else{
            Toast.makeText( getApplicationContext(), "GPS is Disabled.Please enable GPS", Toast.LENGTH_SHORT ).show();   
        }

    }
}





}

And here is my CSVWriter Class:

package com.willardcuizon.differentialgps;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class CSVFileWriter {

private PrintWriter csvWriter;    
private File file;

public CSVFileWriter(File file) {
this.file = file;

}

public void writeHeader(String data) {

try {
    if (data != null) {

        csvWriter = new PrintWriter(new FileWriter(file, true));
        csvWriter.print(",");
        csvWriter.print(data);
        csvWriter.close();

    }
 } catch (IOException e) {
    e.printStackTrace();
 }

}
}
Willard
  • 89
  • 1
  • 11

2 Answers2

6

You should consider using this or this library, as it will take care of writing/parsing csv with has quote as character in their values.

Jaydeep Patel
  • 2,394
  • 1
  • 21
  • 27
1

You can open your csv file as a file and try to read line by line, then you can split the variables by comma and use them to for the next activity. Have a look at the answer here:

Community
  • 1
  • 1
EEE
  • 4,536
  • 3
  • 28
  • 34