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

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

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&lt;String&gt; name= new ArrayList&lt;&gt;(), weight= new ArrayList&lt;&gt;();
@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(&quot;ID_xxxxx.xls&quot;);
Workbook workbook = Workbook.getWorkbook(is);
Sheet s = workbook.getSheet(0);
int r = s.getRows();
for (int i = 0; i &lt; 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&lt;Adapter.ViewHolder&gt; {
LayoutInflater inflater;
List&lt;String&gt; name, weight;
public Adapter(Context context, List&lt;String&gt; name, List&lt;String&gt; 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(&quot;SetTextI18n&quot;)
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.TVname.setText(name.get(position));
if (weight.get(position) == null) {
holder.TVweight.setText(&quot;Poid non renseign&#233;&quot;);
} else {
holder.TVweight.setText(weight.get(position) + &quot; tonnes&quot;);
}
}
@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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_margin=&quot;5dp&quot;
android:background=&quot;#FFF7D0&quot;&gt;
&lt;TextView
android:id=&quot;@+id/SNom&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;8dp&quot;
android:layout_marginBottom=&quot;4dp&quot;
android:text=&quot;Nom&quot;
android:textAlignment=&quot;center&quot;
android:textAllCaps=&quot;false&quot;
android:textColor=&quot;#000000&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;bold&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/Single_Weight&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/Single_Weight&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginBottom=&quot;8dp&quot;
android:text=&quot;Poid&quot;
android:textAlignment=&quot;center&quot;
android:textAllCaps=&quot;false&quot;
android:textColor=&quot;#000000&quot;
android:textSize=&quot;14sp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.0&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/SNom&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

activity_main.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.MainActivity&quot;&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/Listxxxxx&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;0dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&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:

确定