英文:
Nothing appears when I try to read a local excel file in a recyclerview. What can resolve this problem?
问题
以下是您提供的代码的翻译部分:
MainActivity.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 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();
recyclerView = findViewById(R.id.Listxxxxx);
adapter = new Adapter(this, name, weight);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
public void startXLFilereading() {
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>
请注意,这只是您提供的代码的翻译部分,不包括问题和其他不必要的内容。
英文:
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
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>
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论