I’m creating an app which has a csvfile stored in its Raw resource folder and when I click on a button in main Activity , it should print the data of the csvfile in second Activity. But when I launch the app and then launch the second activity using a button ,its output is blank…
I have tried using various codes from stack overflow but I was not able to achieve the desired result. My csv File is really long, but I have kept a portion of it in here , so pls bear with me 😉 :
6200,30,10,9 6200 N,30,10,9 6200 NR,30,10,9 6200 ZNR,30,10,9 6200 ZZNR,30,10,9 6300,35,10,11 6300 2RS,35,10,11 6300 RS,35,10,11 6300 Z,35,10,11 6300 ZZ,35,10,11 6201,32,12,10 6201 2RS,32,12,10 6201 N,32,12,10 6201 NR,32,12,10 6201 RS,32,12,10 6201 Z,32,12,10 6201 ZNR,32,12,10 6201 ZZ,32,12,10
This is my code:-
MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button1 = findViewById(R.id.button1); Button button2 = findViewById(R.id.button2); Button button3 = findViewById(R.id.button3); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SecondActivity(); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ThirdActivity(); } }); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FourthActivity(); } }); } public void SecondActivity() { Intent intent = new Intent(this, SecondActivity.class); startActivity(intent); } public void ThirdActivity(){ Intent intent = new Intent(this, ThirdActivity.class); startActivity(intent); } public void FourthActivity(){ Intent intent = new Intent(this,FourthActivity.class); startActivity(intent); }
SecondActivity.java
public class SecondActivity extends AppCompatActivity { InputStream inputStream; String[] data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); inputStream = getResources().openRawResource(R.raw.bearingdata); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); try { String csvLine; while ((csvLine = reader.readLine()) != null) { data=csvLine.split(","); try{ Log.e("Data ",""+data[0]) ; Log.e("Data ",""+data[0]+""+data[1]+""+data[2]) ; }catch (Exception e){ Log.e("Problem",e.toString()); } } } catch (IOException ex) { throw new RuntimeException("Error in reading CSV file: "+ex); } }
So am I missing something like a create a separate java class for the csv file or is some of my code missing something??? Pls help , because I am just trying to learn some coding and also do this correctly.
Also by the way, I want to display the csv file in a list view on SecondActivity.
Advertisement
Answer
@Nachiketa,
I have tried your shared code and modified using asset manager and able to see the data in the logcat. Please see the below code,
AssetManager am = getAssets(); try { InputStream is = am.open("one.csv"); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String csvLine; while ((csvLine = reader.readLine()) != null) { data=csvLine.split(","); try{ Log.e("Data ",""+data[0]) ; Log.e("Data ",""+data[0]+""+data[1]+""+data[2]) ; }catch (Exception e){ Log.e("Problem",e.toString()); } } } catch (IOException e) { e.printStackTrace(); }
Please check this and mark as answered, if solves your problem.
Thanks