许多应用程序必须在其应用程序中显示统计数据,并将所有数据存储在excel文件或电子表格中。但是,并非每次都可能将整个数据添加到数据库中以在我们的应用程序中使用。在本文中,我们将看一下如何从Android Studio中的Android App中的Excel工作表中读取此数据。
我们将在本文中构建什么?
我们将构建一个简单的应用程序,其中将显示我们已经创建的excel工作表中的数据,并且将读取该excel工作表中的条目并在“回收者”视图中显示该数据列表。下面提供了一个示例视频,以使您对我们在本文中将要做的事情有个大概的了解。注意,我们将使用Java语言实现该项目。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
步骤2:在您的build.gradle文件中添加以下依赖项
以下是Volley的依赖关系,我们将使用它们来从API获取数据。要添加此依赖关系,请导航至应用程序> Gradle脚本> build.gradle(app),然后在“依赖关系”部分添加以下依赖关系。
implementation ‘com.android.volley:volley:1.1.1’
// below line is use for image loading library
implementation ‘com.squareup.picasso:picasso:2.71828’
添加此依赖项后,同步您的项目,现在移至AndroidManifest.xml部分。
步骤3:将互联网权限添加到AndroidManifest.xml文件
导航至应用程序> AndroidManifest.xml,然后将以下代码添加到其中。
XML
XML
Java
public class UserModal {
// variables for our first name,
// last name, email and avatar
private String first_name;
private String last_name;
private String email;
private String avatar;
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public UserModal(String first_name, String last_name, String email, String avatar) {
this.first_name = first_name;
this.last_name = last_name;
this.email = email;
this.avatar = avatar;
}
}
XML
Java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class UserRVAdapter extends RecyclerView.Adapter {
// variable for our array list and context.
private ArrayList userModalArrayList;
private Context context;
// creating a constructor.
public UserRVAdapter(ArrayList userModalArrayList, Context context) {
this.userModalArrayList = userModalArrayList;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// inflating our layout file on below line.
View view = LayoutInflater.from(context).inflate(R.layout.user_rv_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
// getting data from our array list in our modal class.
UserModal userModal = userModalArrayList.get(position);
// on the below line we are setting data to our text view.
holder.firstNameTV.setText(userModal.getFirst_name());
holder.lastNameTV.setText(userModal.getLast_name());
holder.emailTV.setText(userModal.getEmail());
// on below line we are loading our image from the URL
// in our image view using Picasso.
Picasso.get().load(userModal.getAvatar()).into(holder.userIV);
}
@Override
public int getItemCount() {
// returning the size of array list.
return userModalArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
// creating a variable for our text view and image view.
private TextView firstNameTV, lastNameTV, emailTV;
private ImageView userIV;
public ViewHolder(@NonNull View itemView) {
super(itemView);
// initializing our variables.
firstNameTV = itemView.findViewById(R.id.idTVFirstName);
lastNameTV = itemView.findViewById(R.id.idTVLastName);
emailTV = itemView.findViewById(R.id.idTVEmail);
userIV = itemView.findViewById(R.id.idIVUser);
}
}
}
Java
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
// creating a variable for our array list, adapter class,
// recycler view, progressbar, nested scroll view
private ArrayList userModalArrayList;
private UserRVAdapter userRVAdapter;
private RecyclerView userRV;
private ProgressBar loadingPB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// creating a new array list.
userModalArrayList = new ArrayList<>();
// initializing our views.
userRV = findViewById(R.id.idRVUsers);
loadingPB = findViewById(R.id.idPBLoading);
// calling a method to load our API.
getDataFromAPI();
}
private void getDataFromAPI() {
// creating a string variable for URL.
String url = "https://spreadsheets.google.com/feeds/list/1AOOaz-5PhVgIvfROammZsdUs92PdYhEUgGoDrYlGGhc/od6/public/values?alt=json";
// creating a new variable for our request queue
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
// creating a variable for our JSON object request and passing our URL to it.
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
loadingPB.setVisibility(View.GONE);
try {
JSONObject feedObj = response.getJSONObject("feed");
JSONArray entryArray = feedObj.getJSONArray("entry");
for(int i=0; i
第4步:创建用于从Google Spreadsheet提取数据的URL
创建一个简单的Google Spreadsheet,如下所示。确保使用以下文件中所示的相同标题。
创建我们的excel文件之后。现在,我们必须发布此excel文件以在我们的应用程序中使用它。用于发布。
现在,我们将创建一个URL,在其中将以JSON格式获取数据。转到您的Excel工作表,然后复制工作表的ID,如下面的屏幕截图所示。
https://spreadsheets.google.com/feeds/list/“Enter your ID here”/od6/public/values?alt=json
复制此ID后,将ID粘贴在以下URL中,然后在浏览器中运行该URL。您将看到JSON格式的所有Excel数据。现在,我们将在我们的应用程序中以JSON格式使用此数据。确保您已经发布了Excel工作表。否则,此方法将不起作用。现在,我们将在应用程序中使用此URL来获取JSON格式的数据。
步骤5:使用activity_main.xml文件
导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。
XML格式
第6步:创建用于存储数据的Modal类
为了存储我们的数据,我们必须创建一个新的Java类。要创建新的Java类,请导航至应用程序> Java >应用程序的包名称>右键单击它>新建> Java类,并将其命名为UserModal,然后将以下代码添加到其中。
Java
public class UserModal {
// variables for our first name,
// last name, email and avatar
private String first_name;
private String last_name;
private String email;
private String avatar;
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public UserModal(String first_name, String last_name, String email, String avatar) {
this.first_name = first_name;
this.last_name = last_name;
this.email = email;
this.avatar = avatar;
}
}
第7步:为我们的RecyclerView的每个项目创建一个布局文件
导航到应用程序> res>布局>右键单击它>新建>布局资源文件,并将文件名指定为user_rv_item并在其中添加以下代码。
XML格式
步骤8:创建一个Adapter类以将数据设置到我们的RecyclerView项
用于创建新的Adapter类。导航到应用程序> Java >应用程序的程序包名称>右键单击它>新建> Java类,并将其命名为UserRVAdapter,然后将以下代码添加到其中。
Java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class UserRVAdapter extends RecyclerView.Adapter {
// variable for our array list and context.
private ArrayList userModalArrayList;
private Context context;
// creating a constructor.
public UserRVAdapter(ArrayList userModalArrayList, Context context) {
this.userModalArrayList = userModalArrayList;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// inflating our layout file on below line.
View view = LayoutInflater.from(context).inflate(R.layout.user_rv_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
// getting data from our array list in our modal class.
UserModal userModal = userModalArrayList.get(position);
// on the below line we are setting data to our text view.
holder.firstNameTV.setText(userModal.getFirst_name());
holder.lastNameTV.setText(userModal.getLast_name());
holder.emailTV.setText(userModal.getEmail());
// on below line we are loading our image from the URL
// in our image view using Picasso.
Picasso.get().load(userModal.getAvatar()).into(holder.userIV);
}
@Override
public int getItemCount() {
// returning the size of array list.
return userModalArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
// creating a variable for our text view and image view.
private TextView firstNameTV, lastNameTV, emailTV;
private ImageView userIV;
public ViewHolder(@NonNull View itemView) {
super(itemView);
// initializing our variables.
firstNameTV = itemView.findViewById(R.id.idTVFirstName);
lastNameTV = itemView.findViewById(R.id.idTVLastName);
emailTV = itemView.findViewById(R.id.idTVEmail);
userIV = itemView.findViewById(R.id.idIVUser);
}
}
}
步骤9:使用MainActivity。 Java文件
转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
// creating a variable for our array list, adapter class,
// recycler view, progressbar, nested scroll view
private ArrayList userModalArrayList;
private UserRVAdapter userRVAdapter;
private RecyclerView userRV;
private ProgressBar loadingPB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// creating a new array list.
userModalArrayList = new ArrayList<>();
// initializing our views.
userRV = findViewById(R.id.idRVUsers);
loadingPB = findViewById(R.id.idPBLoading);
// calling a method to load our API.
getDataFromAPI();
}
private void getDataFromAPI() {
// creating a string variable for URL.
String url = "https://spreadsheets.google.com/feeds/list/1AOOaz-5PhVgIvfROammZsdUs92PdYhEUgGoDrYlGGhc/od6/public/values?alt=json";
// creating a new variable for our request queue
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
// creating a variable for our JSON object request and passing our URL to it.
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
loadingPB.setVisibility(View.GONE);
try {
JSONObject feedObj = response.getJSONObject("feed");
JSONArray entryArray = feedObj.getJSONArray("entry");
for(int i=0; i
现在运行您的应用程序,并查看该应用程序的输出。
输出: