当我试图在回收视图中读取本地excel文件时,什么都没有出现.什么可以解决这个问题



我是一名年轻的法国开发人员,还没有真正学会编码,但我做得很好。

问题是,我正在开发一个应用程序,它可以读取(和写入(Excel电子表格中的数据,但什么都没有显示。

经过几次搜索,我找到了两个教程,我从中得到了编写代码的灵感,但问题是我的应用程序没有显示任何内容。它应该显示excel文件中已经存在的各种数据的列表(在回收视图中(。excel文件是xls格式的,但什么都不显示,你知道该怎么办吗?

教程链接(在recycleview中在线查看Excel文件(:[此处]1

链接到excel文件阅读教程:〔这里〕2

我不想要在线文件,这可能吗?如果可能,怎么做?提前谢谢。

我的代码:

主要活动.java

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.view.View;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
Adapter adapter;
List<String> name= new ArrayList<>(), weight= new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startXLFilereading();
//initialisation du recyclerView (Liste des moules)
recyclerView = findViewById(R.id.Listxxxxx);
adapter = new Adapter(this, name, weight);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
public void startXLFilereading(){
//Lecture du fichier exel (.xls)
try {
AssetManager am = getAssets();
InputStream is = am.open("ID_xxxxx.xls");
Workbook workbook = Workbook.getWorkbook(is);
Sheet s = workbook.getSheet(0);
int r = s.getRows();
for (int i = 0; i < r; i++){
Cell[] row = s.getRow(i);
name.add(row[0].getContents());
weight.add(row[1].getContents());
}
} catch (Exception e){
}
}
}

Adapter.java

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
LayoutInflater inflater;
List<String> name, weight;
public Adapter(Context context, List<String> name, List<String> weight) {
this.inflater = LayoutInflater.from(context);
this.name = name;
this.weight = weight;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.single_xxxx_layout, parent, false);
return new ViewHolder(view);
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.TVname.setText(name.get(position));
if (weight.get(position) == null) {
holder.TVweight.setText("Poid non renseigné");
} else {
holder.TVweight.setText(weight.get(position) + " tonnes");
}
}
@Override
public int getItemCount() {
return name.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView TVname, TVweight;
public ViewHolder(@NonNull View itemView) {
super(itemView);
TVname = itemView.findViewById(R.id.SNom);
TVweight = itemView.findViewById(R.id.Single_Weight);
}
}
}

single_xxxx_layout.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="wrap_content"
android:layout_margin="5dp"
android:background="#FFF7D0">
<TextView
android:id="@+id/SNom"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="4dp"
android:text="Nom"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/Single_Weight"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/Single_Weight"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Poid"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#000000"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/SNom" />
</androidx.constraintlayout.widget.ConstraintLayout>

activity_main.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=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/Listxxxxx"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

我终于发现了问题。我的xls文件不是真正的.xls格式,但名称是:MyFile.xls.xlxs

我刚刚用.xls格式重新创建了我的文件,它很好。无论如何都要感谢TarikWeiss

相关内容

  • 没有找到相关文章

最新更新