Nothing appears when I try to read a local excel file in a recyclerview. What can resolve this problem?

huangapple go评论104阅读模式
英文:

Nothing appears when I try to read a local excel file in a recyclerview. What can resolve this problem?

问题

以下是您提供的代码的翻译部分:

MainActivity.java

  1. import androidx.appcompat.app.AppCompatActivity;
  2. import androidx.recyclerview.widget.LinearLayoutManager;
  3. import androidx.recyclerview.widget.RecyclerView;
  4. import android.content.res.AssetManager;
  5. import android.os.Bundle;
  6. import java.io.InputStream;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import jxl.Cell;
  10. import jxl.Sheet;
  11. import jxl.Workbook;
  12. public class MainActivity extends AppCompatActivity {
  13. RecyclerView recyclerView;
  14. Adapter adapter;
  15. List<String> name = new ArrayList<>(), weight = new ArrayList<>();
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20. startXLFilereading();
  21. recyclerView = findViewById(R.id.Listxxxxx);
  22. adapter = new Adapter(this, name, weight);
  23. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  24. recyclerView.setAdapter(adapter);
  25. }
  26. public void startXLFilereading() {
  27. try {
  28. AssetManager am = getAssets();
  29. InputStream is = am.open("ID_xxxxx.xls");
  30. Workbook workbook = Workbook.getWorkbook(is);
  31. Sheet s = workbook.getSheet(0);
  32. int r = s.getRows();
  33. for (int i = 0; i < r; i++) {
  34. Cell[] row = s.getRow(i);
  35. name.add(row[0].getContents());
  36. weight.add(row[1].getContents());
  37. }
  38. } catch (Exception e) {
  39. // 处理异常
  40. }
  41. }
  42. }

Adapter.java

  1. import android.annotation.SuppressLint;
  2. import android.content.Context;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.TextView;
  7. import androidx.annotation.NonNull;
  8. import androidx.recyclerview.widget.RecyclerView;
  9. import java.util.List;
  10. public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
  11. LayoutInflater inflater;
  12. List<String> name, weight;
  13. public Adapter(Context context, List<String> name, List<String> weight) {
  14. this.inflater = LayoutInflater.from(context);
  15. this.name = name;
  16. this.weight = weight;
  17. }
  18. @NonNull
  19. @Override
  20. public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  21. View view = inflater.inflate(R.layout.single_xxxx_layout, parent, false);
  22. return new ViewHolder(view);
  23. }
  24. @SuppressLint("SetTextI18n")
  25. @Override
  26. public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  27. holder.TVname.setText(name.get(position));
  28. if (weight.get(position) == null) {
  29. holder.TVweight.setText("Poid non renseigné");
  30. } else {
  31. holder.TVweight.setText(weight.get(position) + " tonnes");
  32. }
  33. }
  34. @Override
  35. public int getItemCount() {
  36. return name.size();
  37. }
  38. public class ViewHolder extends RecyclerView.ViewHolder {
  39. TextView TVname, TVweight;
  40. public ViewHolder(@NonNull View itemView) {
  41. super(itemView);
  42. TVname = itemView.findViewById(R.id.SNom);
  43. TVweight = itemView.findViewById(R.id.Single_Weight);
  44. }
  45. }
  46. }

single_xxxx_layout.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:layout_margin="5dp"
  9. android:background="#FFF7D0">
  10. <TextView
  11. android:id="@+id/SNom"
  12. android:layout_width="0dp"
  13. android:layout_height="wrap_content"
  14. android:layout_marginTop="8dp"
  15. android:layout_marginBottom="4dp"
  16. android:text="Nom"
  17. android:textAlignment="center"
  18. android:textAllCaps="false"
  19. android:textColor="#000000"
  20. android:textSize="18sp"
  21. android:textStyle="bold"
  22. app:layout_constraintBottom_toTopOf="@+id/Single_Weight"
  23. app:layout_constraintEnd_toEndOf="parent"
  24. app:layout_constraintStart_toStartOf="parent"
  25. app:layout_constraintTop_toTopOf="parent" />
  26. <TextView
  27. android:id="@+id/Single_Weight"
  28. android:layout_width="0dp"
  29. android:layout_height="wrap_content"
  30. android:layout_marginBottom="8dp"
  31. android:text="Poid"
  32. android:textAlignment="center"
  33. android:textAllCaps="false"
  34. android:textColor="#000000"
  35. android:textSize="14sp"
  36. app:layout_constraintBottom_toBottomOf="parent"
  37. app:layout_constraintEnd_toEndOf="parent"
  38. app:layout_constraintHorizontal_bias="0.0"
  39. app:layout_constraintStart_toStartOf="parent"
  40. app:layout_constraintTop_toBottomOf="@+id/SNom" />
  41. </androidx.constraintlayout.widget.ConstraintLayout>

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MainActivity">
  9. <androidx.recyclerview.widget.RecyclerView
  10. android:id="@+id/Listxxxxx"
  11. android:layout_width="0dp"
  12. android:layout_height="0dp"
  13. app:layout_constraintBottom_toBottomOf="parent"
  14. app:layout_constraintEnd_toEndOf="parent"
  15. app:layout_constraintStart_toStartOf="parent"
  16. app:layout_constraintTop_toTopOf="parent" />
  17. </androidx.constraintlayout.widget.ConstraintLayout>

请注意,这只是您提供的代码的翻译部分,不包括问题和其他不必要的内容。

英文:

I'm a young French developer who hasn't really learned coding but I'm doing pretty well anyway.

The problem is that I am developing an application that can read (and write) data to an Excel spreadsheet but nothing is showing.

After several searches, I found 2 tutorials from which I was inspired to write my code but the problem is that my application does not display anything. It is supposed to display a list (in a recyclerview) of the various data already present on the excel file.
The excel file is in xls but nothing is displayed, would you know what to do?

Tutorial link (view an Excel file online in recyclview): [Here] 1

Link to the excel file reading tutorial: [Here] 2

I don't want an online file, is this possible?
if possible, how? Thank you in advance.

My code :

MainActivity.java

  1. import androidx.appcompat.app.AppCompatActivity;
  2. import androidx.recyclerview.widget.LinearLayoutManager;
  3. import androidx.recyclerview.widget.RecyclerView;
  4. import android.content.res.AssetManager;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import java.io.InputStream;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import jxl.Cell;
  11. import jxl.Sheet;
  12. import jxl.Workbook;
  13. public class MainActivity extends AppCompatActivity {
  14. RecyclerView recyclerView;
  15. Adapter adapter;
  16. List&lt;String&gt; name= new ArrayList&lt;&gt;(), weight= new ArrayList&lt;&gt;();
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. startXLFilereading();
  22. //initialisation du recyclerView (Liste des moules)
  23. recyclerView = findViewById(R.id.Listxxxxx);
  24. adapter = new Adapter(this, name, weight);
  25. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  26. recyclerView.setAdapter(adapter);
  27. }
  28. public void startXLFilereading(){
  29. //Lecture du fichier exel (.xls)
  30. try {
  31. AssetManager am = getAssets();
  32. InputStream is = am.open(&quot;ID_xxxxx.xls&quot;);
  33. Workbook workbook = Workbook.getWorkbook(is);
  34. Sheet s = workbook.getSheet(0);
  35. int r = s.getRows();
  36. for (int i = 0; i &lt; r; i++){
  37. Cell[] row = s.getRow(i);
  38. name.add(row[0].getContents());
  39. weight.add(row[1].getContents());
  40. }
  41. } catch (Exception e){
  42. }
  43. }
  44. }

Adapter.java

  1. import android.annotation.SuppressLint;
  2. import android.content.Context;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.TextView;
  7. import androidx.annotation.NonNull;
  8. import androidx.recyclerview.widget.RecyclerView;
  9. import java.util.List;
  10. public class Adapter extends RecyclerView.Adapter&lt;Adapter.ViewHolder&gt; {
  11. LayoutInflater inflater;
  12. List&lt;String&gt; name, weight;
  13. public Adapter(Context context, List&lt;String&gt; name, List&lt;String&gt; weight) {
  14. this.inflater = LayoutInflater.from(context);
  15. this.name = name;
  16. this.weight = weight;
  17. }
  18. @NonNull
  19. @Override
  20. public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  21. View view = inflater.inflate(R.layout.single_xxxx_layout, parent, false);
  22. return new ViewHolder(view);
  23. }
  24. @SuppressLint(&quot;SetTextI18n&quot;)
  25. @Override
  26. public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  27. holder.TVname.setText(name.get(position));
  28. if (weight.get(position) == null) {
  29. holder.TVweight.setText(&quot;Poid non renseign&#233;&quot;);
  30. } else {
  31. holder.TVweight.setText(weight.get(position) + &quot; tonnes&quot;);
  32. }
  33. }
  34. @Override
  35. public int getItemCount() {
  36. return name.size();
  37. }
  38. public class ViewHolder extends RecyclerView.ViewHolder {
  39. TextView TVname, TVweight;
  40. public ViewHolder(@NonNull View itemView) {
  41. super(itemView);
  42. TVname = itemView.findViewById(R.id.SNom);
  43. TVweight = itemView.findViewById(R.id.Single_Weight);
  44. }
  45. }
  46. }

single_xxxx_layout.xml

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.constraintlayout.widget.ConstraintLayout
  3. xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  4. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  5. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  6. android:layout_width=&quot;match_parent&quot;
  7. android:layout_height=&quot;wrap_content&quot;
  8. android:layout_margin=&quot;5dp&quot;
  9. android:background=&quot;#FFF7D0&quot;&gt;
  10. &lt;TextView
  11. android:id=&quot;@+id/SNom&quot;
  12. android:layout_width=&quot;0dp&quot;
  13. android:layout_height=&quot;wrap_content&quot;
  14. android:layout_marginTop=&quot;8dp&quot;
  15. android:layout_marginBottom=&quot;4dp&quot;
  16. android:text=&quot;Nom&quot;
  17. android:textAlignment=&quot;center&quot;
  18. android:textAllCaps=&quot;false&quot;
  19. android:textColor=&quot;#000000&quot;
  20. android:textSize=&quot;18sp&quot;
  21. android:textStyle=&quot;bold&quot;
  22. app:layout_constraintBottom_toTopOf=&quot;@+id/Single_Weight&quot;
  23. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  24. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  25. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  26. &lt;TextView
  27. android:id=&quot;@+id/Single_Weight&quot;
  28. android:layout_width=&quot;0dp&quot;
  29. android:layout_height=&quot;wrap_content&quot;
  30. android:layout_marginBottom=&quot;8dp&quot;
  31. android:text=&quot;Poid&quot;
  32. android:textAlignment=&quot;center&quot;
  33. android:textAllCaps=&quot;false&quot;
  34. android:textColor=&quot;#000000&quot;
  35. android:textSize=&quot;14sp&quot;
  36. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  37. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  38. app:layout_constraintHorizontal_bias=&quot;0.0&quot;
  39. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  40. app:layout_constraintTop_toBottomOf=&quot;@+id/SNom&quot; /&gt;
  41. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

activity_main.xml

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.constraintlayout.widget.ConstraintLayout
  3. xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  4. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  5. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  6. android:layout_width=&quot;match_parent&quot;
  7. android:layout_height=&quot;match_parent&quot;
  8. tools:context=&quot;.MainActivity&quot;&gt;
  9. &lt;androidx.recyclerview.widget.RecyclerView
  10. android:id=&quot;@+id/Listxxxxx&quot;
  11. android:layout_width=&quot;0dp&quot;
  12. android:layout_height=&quot;0dp&quot;
  13. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  14. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  15. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  16. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  17. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

答案1

得分: 1

我终于找到问题所在。我的xls文件实际上并不是 .xls 格式,而是有这个名字:MyFile.xls.xlxs

我刚刚重新创建了我的文件,使用 .xls 格式,问题解决了。无论如何还是要感谢TarikWeiss。

英文:

I finally found the problem. My xls file was not really in .xls format but had this name: MyFile.xls.xlxs

I just recreated my file in .xls format and it's good. Thanks anyway to TarikWeiss

huangapple
  • 本文由 发表于 2020年7月26日 07:23:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63094570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定