单击Cardview时从Firebase Firestore获取数据并使用highcharts绘制直方图-Android

时间:2018-09-27 15:02:14

标签: android firebase highcharts google-cloud-firestore android-cardview

我有一个活动,其中有许多卡视图,单击这些卡视图应从Firebase Firestore获取数据并使用android中的highcharts以直方图格式绘制数据。在每个卡视图单击事件中,该卡视图的d都会传递在我的情况下,卡片视图仅在第二次单击时起作用,并且在单击其他卡片视图时,它仍保留上一次查询的值,并且仅在第二次单击时才显示正确数据。

下面是我的适配器视图代码,其中包含cardviews。

   DocumentReference docRef = rootRef.collection("Users").document(tId).collection("Subjects")
                .document(subId).collection("Marks").document(testList.get(position).getMarksID());

           holder.show_graph.setOnClickListener(v -> {

               docRef.get().addOnSuccessListener(documentSnapshot -> {
                   mMarks = new HashSet<>();
                   if (documentSnapshot != null && documentSnapshot.exists()) {
                       Map<String, Object> hm = documentSnapshot.getData();
                       Set<String> a = hm.keySet();
                       for (String b : a) {
                           try {
                               holder.marks_obtained.setText((String)documentSnapshot.get(email));

                               if(!b.equals("Max_marks")){
                                   Log.e( "onComplete: ", documentSnapshot.get(b+".com") + "  " +documentSnapshot.getId() );
                                   mMarks.add(Float.parseFloat((String)documentSnapshot.get(b+".com")));
                               }
                           } catch (Exception e) {
                               e.printStackTrace();
                           }
                       }
                   }
               });

            v = LayoutInflater.from(context).inflate(R.layout.graph_plot,null);
            final View alertLayout = v;

            try{
                //HICharts
                HIChartView chartView =  alertLayout.findViewById(R.id.hc);
                chartView.plugins = new ArrayList<>(Arrays.asList("histogram-bellcurve"));

                HIOptions options = new HIOptions();

                HIChart chart = new HIChart();
                chart.setType("variwide");
                options.setChart(chart);

                HITitle title = new HITitle();
                title.setText("Score Division");
                options.setTitle(title);

                HIXAxis xaxis1 = new HIXAxis();
                HITitle ht = new HITitle();
                ht.setText("Count");
                xaxis1.setTitle(ht);

                HIXAxis xaxis2 = new HIXAxis();
                xaxis2.setTitle(new HITitle());
                xaxis2.setOpposite(true);

                options.setXAxis(new ArrayList<>(Arrays.asList(xaxis1, xaxis2)));

                HIYAxis yaxis1 = new HIYAxis();
                HITitle ht2 = new HITitle();
                ht2.setText("Marks");
                yaxis1.setTitle(ht2);

                HIYAxis yaxis2 = new HIYAxis();
                yaxis2.setTitle(new HITitle());
                yaxis2.setOpposite(true);

                options.setYAxis(new ArrayList<>(Arrays.asList(yaxis1, yaxis2)));

                HILegend legend = new HILegend();
                legend.setEnabled(true);
                options.setLegend(legend);

                HIHistogram series1 = new HIHistogram();
                series1.setType("histogram");
                series1.setName("Histogram");
                series1.setXAxis(1);
                series1.setYAxis(1);
                series1.setBaseSeries("s1");
                series1.setZIndex(-1);

                HIScatter series2 = new HIScatter();
                series2.setType("scatter");
                series2.setName("Data");

                Number[] series2_data = new Number[mMarks.size()];

                int i = 0;
                for(float m : mMarks){
                    series2_data[i] = m;
                    i++;
                }

                series2.setId("s1");
                series2.setData(new ArrayList<>(Arrays.asList(series2_data)));
                series2.setMarker(new HIMarker());
                series2.getMarker().setRadius(2.5);

                options.setSeries(new ArrayList<>(Arrays.asList(series1, series2)));

                options.setExporting(new HIExporting());
                options.getExporting().setEnabled(false);

                chartView.setOptions(options);

                AlertDialog.Builder alertBox = new AlertDialog.Builder(v.getRootView().getContext());
                alertBox.setTitle("Graph");

                alertBox.setView(alertLayout);
                alertBox.setCancelable(false);

                alertBox.setPositiveButton("Done", (dialog, which) -> dialog.dismiss());

                AlertDialog dialog = alertBox.create();
                dialog.show();
            }
            catch (NullPointerException e){
                Log.e("",e.getLocalizedMessage());
            }
            finally {
                if(mMarks != null){
                    mMarks.clear();
                }
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

正如@daniel_s提到的那样,highcharts并不是问题。而且它也不是firebase的问题。这就是您放置代码的方式。

现在,我不是合适的人谈论Firebase,但让我们理解这一点。 Firebase使用异步机制,并且Firebase的所有方法均为异步类型。因此,当您单击Cardview时,firebase方法和highchart方法同时运行,而不是一个又一个运行。

要按照您的方式进行此工作,您应该执行以下操作:

holder.show_graph.setOnClickListener(v -> {

               docRef.get().addOnSuccessListener(documentSnapshot -> {
                   mMarks = new HashSet<>();
                   if (documentSnapshot != null && documentSnapshot.exists()) {
                       Map<String, Object> hm = documentSnapshot.getData();
                       Set<String> a = hm.keySet();
                       for (String b : a) {
                           try {
                               holder.marks_obtained.setText((String)documentSnapshot.get(email));

                               if(!b.equals("Max_marks")){
                                   Log.e( "onComplete: ", documentSnapshot.get(b+".com") + "  " +documentSnapshot.getId() );
                                   mMarks.add(Float.parseFloat((String)documentSnapshot.get(b+".com")));
                               }
                           } catch (Exception e) {
                               e.printStackTrace();
                           }
                       }
            // All of your highcharts code here.....
            v = LayoutInflater.from(context).inflate(R.layout.graph_plot,null);
            final View alertLayout = v;

            try{
                //HICharts
                HIChartView chartView =  alertLayout.findViewById(R.id.hc);
                chartView.plugins = new ArrayList<>(Arrays.asList("histogram-bellcurve"));

                HIOptions options = new HIOptions();

                HIChart chart = new HIChart();
                chart.setType("variwide");
                options.setChart(chart);

                HITitle title = new HITitle();
                title.setText("Score Division");
                options.setTitle(title);

                HIXAxis xaxis1 = new HIXAxis();
                HITitle ht = new HITitle();
                ht.setText("Count");
                xaxis1.setTitle(ht);

                HIXAxis xaxis2 = new HIXAxis();
                xaxis2.setTitle(new HITitle());
                xaxis2.setOpposite(true);

                options.setXAxis(new ArrayList<>(Arrays.asList(xaxis1, xaxis2)));

                HIYAxis yaxis1 = new HIYAxis();
                HITitle ht2 = new HITitle();
                ht2.setText("Marks");
                yaxis1.setTitle(ht2);

                HIYAxis yaxis2 = new HIYAxis();
                yaxis2.setTitle(new HITitle());
                yaxis2.setOpposite(true);

                options.setYAxis(new ArrayList<>(Arrays.asList(yaxis1, yaxis2)));

                HILegend legend = new HILegend();
                legend.setEnabled(true);
                options.setLegend(legend);

                HIHistogram series1 = new HIHistogram();
                series1.setType("histogram");
                series1.setName("Histogram");
                series1.setXAxis(1);
                series1.setYAxis(1);
                series1.setBaseSeries("s1");
                series1.setZIndex(-1);

                HIScatter series2 = new HIScatter();
                series2.setType("scatter");
                series2.setName("Data");

                Number[] series2_data = new Number[mMarks.size()];

                int i = 0;
                for(float m : mMarks){
                    series2_data[i] = m;
                    i++;
                }

                series2.setId("s1");
                series2.setData(new ArrayList<>(Arrays.asList(series2_data)));
                series2.setMarker(new HIMarker());
                series2.getMarker().setRadius(2.5);

                options.setSeries(new ArrayList<>(Arrays.asList(series1, series2)));

                options.setExporting(new HIExporting());
                options.getExporting().setEnabled(false);

                chartView.setOptions(options);

                AlertDialog.Builder alertBox = new AlertDialog.Builder(v.getRootView().getContext());
                alertBox.setTitle("Graph");

                alertBox.setView(alertLayout);
                alertBox.setCancelable(false);

                alertBox.setPositiveButton("Done", (dialog, which) -> dialog.dismiss());

                AlertDialog dialog = alertBox.create();
                dialog.show();
            }
            catch (NullPointerException e){
                Log.e("",e.getLocalizedMessage());
            }
            finally {
                if(mMarks != null){
                    mMarks.clear();
                }
            }
                   }
               });

希望我很清楚。如果有效,请单击对勾接受我的回答:)