如何在Android中读取和显示csv文件中的数据?
我试图理解如何从csv文件中读取并在android studio的活动中显示它。例如,该文件可以包含以下信息:如何在Android中读取和显示csv文件中的数据?,android,android-activity,Android,Android Activity,我试图理解如何从csv文件中读取并在android studio的活动中显示它。例如,该文件可以包含以下信息: name,employer,location,position type,core competency Junior Data Analyst,Lockerdome,Saint Louis,Data Scientist / Business Intelligence, Statistical Analysis Project Coordinator Support,Maritz,S
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
对许多人来说,这可能是一项简单的任务,但我对Android开发还相当陌生。我看过一些教程,但它们都有不同的做法,其中一些比较老。让我困惑的是,一个人用Java创建一个类,而另一个人创建一个活动。更让我困惑的是,另一个人在Android中使用MVC模式。我知道这在.NET中是有意义的。
但是有人能告诉我从哪里开始吗?遵循哪些结构?我知道从活动开始是最好的做法。例如,jobListActivity
非常感谢您为我提供任何新的好教程或示例代码的链接
编辑(评论后):
因此,我创建了一个ListActivity,它应该显示从CSV文件读取的作业列表。
JobListActivity类,具有作业的属性
我仍然无法显示csv文件中的作业列表。
这是我的密码:
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();
}
}
}
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类
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;
}
}
你确实很困惑,把一些事情搞混了。让我们一步一步走 一步一步,一个问题一个问题
- 类是一种Java形式李>
- 对于应用程序中的每个屏幕,您将有一个 不同的活动。他们每个人都可能有不止一个类
implementation 'com.opencsv:opencsv:4.6'
然后修改onCreate(或任何您想要读取csv的地方):
最后
对,就是这样。如果您还想在列表、网格等中表示这些数据,那么,这是另一个问题!但是,为了以防万一,您只需要再次使用您创建的活动,但将ListView添加到布局中,并在onCreate中提供列表(例如)。请参阅教程。试试——它会让你的生活更轻松
首先,将此包添加到gradle依赖项中,如下所示
implementation 'com.opencsv:opencsv:4.6'
那你可以
import com.opencsv.CSVReader;
import java.io.IOException;
import java.io.FileReader;
...
try {
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
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 (IOException e) {
}
or
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
List myEntries = reader.readAll();
Edit after comment
try {
File csvfile = new File(Environment.getExternalStorageDirectory() + "/csvfile.csv");
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();
}
String csvfileString = this.getApplicationInfo().dataDir + File.separatorChar + "csvfile.csv"
File csvfile = new File(csvfileString);
如果要将.csv文件与应用程序打包,并在应用程序安装时将其安装在内部存储器上,请在项目src/main文件夹(例如c:\myapp\app\src\main\assets)中创建一个assets文件夹,并将.csv文件放入其中,然后在活动中这样引用它:您可以像读取任何其他文件一样读取此类文件,并将内容放入字符串变量中。。首先,可以在文本视图中显示文本。对于其余部分,您没有告诉如何在文件中显示数据。非常感谢@Miquel。像你们这样的人可以让我这样的初学者的生活更轻松,编码也更有趣:)现在我更开明了。谢谢!我现在编辑了答案,为第5步添加了更多的细节!!!嘿你的代码有效吗?如果没有,它是否正确读取文件?让我们试着找出问题的确切位置。。。请将答案标记为已解决您的问题的答案:将不胜感激!while子句中出现错误。它正在抱怨readNext()。以及getApplicationInfo()。。无法解析方法readNext()。谢谢@anupret,下一个答案中也会详细介绍这一点。
@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();
}
}
implementation 'com.opencsv:opencsv:4.6'
import com.opencsv.CSVReader;
import java.io.IOException;
import java.io.FileReader;
...
try {
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
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 (IOException e) {
}
or
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
List myEntries = reader.readAll();
Edit after comment
try {
File csvfile = new File(Environment.getExternalStorageDirectory() + "/csvfile.csv");
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();
}
String csvfileString = this.getApplicationInfo().dataDir + File.separatorChar + "csvfile.csv"
File csvfile = new File(csvfileString);