I'm trying to understand how I can read from a csv file and show it on an activity in android studio. The file can contain for instance these information:
name,employer,location,position type,core competency
Junior Data Analyst,Lockerdome,Saint Louis,Data Scientist / Business Intelligence, Statistical Analysis
Project Coordinator Support,Maritz,Saint Louis,Technical Assistant / User Support,Non-coding
Junior Web Developer,Cozy,Portland,Web - Front End,Ruby
Junior Developer 3,LiveAnswer,South Florida,Web - Full Stack,Java
Full Stack Engineer,Splitwise,Rhode Island,Web - Full Stack,Ruby
Customer Experience,Splitwise,Rhode Island,Project Manager / Analyst,Non-coding
IT Support Specialist,Viamontech,South Florida,Technical Assistant / User Support,Non-coding C#/.net
Developer ,Hunter Engineering,Saint Louis,Software / Enterprise Developer,.Net
Junior Developer,"TruckMovers.com, Inc.",Kansas City,Web - Full Stack,Python
Software Engineer,Computer Associates Inc,Rhode Island,Software / Enterprise Developer,.Net
It probably is a simple task to many, but I am fairly new to Android development. I have seen a few tutorials, but they all do differently and some of them are older. What was confusing for me is that one guy creates a class in Java while another creates an activity. Added more to my confusion is, another guy uses MVC pattern in Android. I know it makes sense in .NET.
But could someone please guide me where shall I start? Which structures to follow? I know that it is best practice to start with activity. For instance a jobListActivity
.
Any link to any good and new tutorials or sample codes so that I can get started is very much appreciated.
EDIT (After comments): So I have created a ListActivity, which should display the list of jobs being read from the CSV file. A JobListActivity class, which has attributes for a job.
Still I am not able to display the list of jobs from the csv file. This is my codes:
MainActivity.java
package com.example.jobsearch;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;
import com.opencsv.CSVReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView list = findViewById(R.id.list);
ArrayList<String> arrayList = new ArrayList<>();
try {
String csvfileString = this.getApplicationInfo().dataDir + File.separatorChar + "jobsCSV.csv";
File csvfile = new File(csvfileString);
CSVReader reader = new CSVReader(new FileReader("csvfile.getAbsolutePath()"));
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
System.out.println(nextLine[0] + nextLine[1] + "etc...");
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "The specified file was not found", Toast.LENGTH_SHORT).show();
}
}
}
activity_list.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ListActivity">
<TextView
android:id="@+id/tvAList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="@string/textViewActivityList"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
listActivity.java
package com.example.jobsearch;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class ListActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
}
}
JobListActivity.java class
package com.example.jobsearch;
import android.widget.ImageView;
public class JobsListActivity {
public String Company;
public String Title;
public String Rating;
public String ApplyStatus;
public ImageView CompanyLogos;
public String Location;
public String Position;
public JobsListActivity(String company,
String title,
String rating,
String applyStatus,
ImageView companyLogos,
String location,
String position) {
Company = company;
Title = title;
Rating = rating;
ApplyStatus = applyStatus;
CompanyLogos = companyLogos;
Location = location;
Position = position;
}
}
You are indeed confused and mixing some things up. Let's go step by step.
STEP BY STEP, QUESTION BY QUESTION
A class and an activity are not the same. You can read about it here and here. To put it in an easy way:
Then, in Android, if you want to do any app (including the one which needs to read the .csv) you will need an activity, so that there is at least one screen in which the user can be. That is to say that the first thing you will need to do is to create an activity and add it to the manifest as the default activity (so that it appears together with the rest of apps). Do this (create activity), and then this (set it as default).
Now you should be able to try the app in an emulator or a physical device and see an empty screen that does nothing.
So far, so good. Now to the .csv reading problem. We will read the code as soon as the user enters the activity, when the activity is created (in the onCreate method). There should be a piece of code like the following one in your activity, otherwise, create it.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
EDIT. Let's deep deeper into this step (5). As stated in the answer I have referenced, you need to:
Add this package to your gradle dependencies as follows
implementation 'com.opencsv:opencsv:4.6'
And then modify the onCreate (or wherever you want to read the csv):
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
// OPTION 1: if the file is in the sd
File csvfile = new File(Environment.getExternalStorageDirectory() + "/csvfile.csv");
// END OF OPTION 1
// OPTION 2: pack the file with the app
/* "If you want to package the .csv file with the application and have it install on the internal storage when the app installs, create an assets folder in your project src/main folder (e.g., c:\myapp\app\src\main\assets\), and put the .csv file in there, then reference it like this in your activity:" (from the cited answer) */
String csvfileString = this.getApplicationInfo().dataDir + File.separatorChar + "csvfile.csv"
File csvfile = new File(csvfileString);
// END OF OPTION 2
CSVReader reader = new CSVReader(new FileReader("csvfile.getAbsolutePath()"));
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
System.out.println(nextLine[0] + nextLine[1] + "etc...");
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "The specified file was not found", Toast.LENGTH_SHORT).show();
}
}
FINALLY
Right, so this would be it. If you also wanted to represent this data in a list, a grid, etc., well, this is another question! However, just in case, you just need to use again the activity you created, but add the ListView to your layout and feed the list also in onCreate (for example). See a tutorial here.