forked from alcarazolabs/Android-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main_Activity.java
80 lines (69 loc) · 2.9 KB
/
Main_Activity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.example.readexcel;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.FileAsyncHttpResponseHandler;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import cz.msebera.android.httpclient.Header;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
public class MainActivity extends AppCompatActivity {
Workbook workbook;
AsyncHttpClient asyncHttpClient;
List<String> titulos,descripciones,imagenes;
private RecyclerView recyclerView;
Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
titulos = new ArrayList<>();
descripciones = new ArrayList<>();
imagenes = new ArrayList<>();
String URL = "https://bikashthapa01.github.io/excel-reader-android-app/story.xls";
asyncHttpClient = new AsyncHttpClient();
asyncHttpClient.get(URL, new FileAsyncHttpResponseHandler(this) {
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, File file) {
Toast.makeText(MainActivity.this, "Error al descargar el archivo Excel", Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(int statusCode, Header[] headers, File file) {
WorkbookSettings ws = new WorkbookSettings();
ws.setGCDisabled(true);
if(file!=null){
try {
workbook = Workbook.getWorkbook(file);
Sheet sheet = workbook.getSheet(0);
for(int i=0;i<sheet.getRows();i++){
Cell[] row = sheet.getRow(0);
titulos.add(row[0].getContents());
descripciones.add(row[1].getContents());
imagenes.add(row[2].getContents());
}
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
mostrarData();
}
}
});
}
private void mostrarData(){
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new Adapter(this,titulos,descripciones,imagenes);
recyclerView.setAdapter(adapter);
}
}