I already have TOPSIS algorithm that can calculate data from an array[][]. Now I’m going to use the data from:
dummy data:
double[][] data= { {887, 475, 4, 128, 186, 3621000}, {887, 475, 8, 128, 189, 4011000}, {1481, 991, 4, 128, 186, 4767000}, {1481, 991, 8, 128, 186, 5157000}, {1481, 991, 8, 256, 189, 5376000}};
to database’s data
dao_pc.get(key).addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot snapshot) { ArrayList<Item_pc> item_pcs = new ArrayList<>(); for (DataSnapshot data : snapshot.getChildren()) { Item_pc item_pc = data.getValue(Item_pc.class); item_pcs.add(item_pc); key = data.getKey(); } adapter_pc = new Adapter_pc(getApplicationContext(), item_pcs); recyclerView.setAdapter(adapter_pc); adapter_pc.notifyDataSetChanged(); } @Override public void onCancelled(@NonNull DatabaseError error) { } });
My Item_pc.class
public class Item_pc { private String cpu; private String gpu; private String ram; private String ssd; private String power; private String harga; public Item_score(String cpu, String gpu, String ram, String ssd, String power, String harga) { this.cpu = cpu; this.gpu = gpu; this.ram = ram; this.ssd = ssd; this.power = power; this.harga = harga; } public Item_pc(){} public String getCpu() {return cpu;} public void setCpu(String cpu) {this.cpu = cpu;} public String getGpu() {return gpu;} public void setGpu(String gpu) {this.gpu = gpu;} public String getRam() {return ram;} public void setRam(String ram) {this.ram = ram;} public String getSsd() {return ssd;} public void setSsd(String ssd) {this.ssd = ssd;} public String getPower() {return power;} public void setPower(String power) {this.power = power;} public String getHarga() {return harga;} public void setHarga(String harga) {this.harga = harga;}
Condition:
- I can get data from firebase using arraylist
- I can calculate data using dummy array with TOPSIS algorithm
problem : how to calculate data from firebase with TOPSIS algorithm? I have trouble that data from firebase need to use Arraylist format, I don’t know how to store data on array[][] like array_push(PHP version) on java android.
Advertisement
Answer
First of all, do not store numbers as Strings in the database nor in your Item_pc
class. If you have the values stored as Strings, change them to int, for example. So your class declaration should look like this:
class Item_pc { private int cpu; private int gpu; private int ram; private int ssd; private int power; private int harga; public Item_pc(int cpu, int gpu, int ram, int ssd, int power, int harga) { this.cpu = cpu; this.gpu = gpu; this.ram = ram; this.ssd = ssd; this.power = power; this.harga = harga; } public Item_pc() { } public int getCpu() { return cpu; } public void setCpu(int cpu) { this.cpu = cpu; } public int getGpu() { return gpu; } public void setGpu(int gpu) { this.gpu = gpu; } public int getRam() { return ram; } public void setRam(int ram) { this.ram = ram; } public int getSsd() { return ssd; } public void setSsd(int ssd) { this.ssd = ssd; } public int getPower() { return power; } public void setPower(int power) { this.power = power; } public int getHarga() { return harga; } public void setHarga(int harga) { this.harga = harga; } }
Assuming that you get from the database a list that looks like this:
ArrayList<Item_pc> item_pcs = new ArrayList<>(); item_pcs.add(new Item_pc(887, 475, 4, 128, 186, 3621000)); item_pcs.add(new Item_pc(887, 475, 8, 128, 189, 4011000)); item_pcs.add(new Item_pc(1481, 991, 4, 128, 186, 4767000)); item_pcs.add(new Item_pc(1481, 991, 8, 128, 186, 5157000)); item_pcs.add(new Item_pc(1481, 991, 8, 256, 189, 5376000));
To create the two-dimensional array, please use the following lines of code:
int[][] data = new int[item_pcs.size()][6]; for (int i = 0; i < data.length; i ++) { Item_pc item_pc = item_pcs.get(i); data[i] = new int[]{ item_pc.getCpu(), item_pc.getGpu(), item_pc.getRam(), item_pc.getSsd(), item_pc.getPower(), item_pc.getHarga() }; } for (int[] d : data) { for (int i : d) { System.out.print(i + ", "); } System.out.println(); }
The result in the logcat will be:
887, 475, 4, 128, 186, 3621000, 887, 475, 8, 128, 189, 4011000, 1481, 991, 4, 128, 186, 4767000, 1481, 991, 8, 128, 186, 5157000, 1481, 991, 8, 256, 189, 5376000,