如何从2个片段中的文本视图中获取条形图值?

时间:2017-11-26 14:29:22

标签: java android android-fragments bar-chart mpandroidchart

我希望将Profile(Fragment2)中的文本视图值存储在Dashboard(Fragment 1)的条形图值中。

我正在使用MPAndroidChart的条形图。

单击按钮时,将从编辑配置文件(片段3)中检索配置文件(片段2)中的文本视图值。

我无法在Dashboard的条形图中显示Profile的文本视图值。我该如何解决这个问题?

这是我的代码:

信息中心(片段1)

public class Dashboard extends Fragment{
    View myView; //A view object called myView

    private BarChart chart;
    float barWidth;
    float barSpace;
    float groupSpace;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.dashboard, container, false);

        //Spinner
        Spinner filter = (Spinner) myView.findViewById(R.id.ddl_filter);
        ArrayAdapter<CharSequence> filterAdapter = ArrayAdapter.createFromResource(this.getActivity(),
                R.array.filterArray, android.R.layout.simple_spinner_item);
        filterAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        filter.setAdapter(filterAdapter);

        Spinner result = (Spinner) myView.findViewById(R.id.ddl_result);
        ArrayAdapter<CharSequence> resultAdapter = ArrayAdapter.createFromResource(this.getActivity(),
                R.array.resultArray, android.R.layout.simple_spinner_item);
        resultAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        result.setAdapter(resultAdapter);

        barWidth = 0.3f;
        barSpace = 0f;
        groupSpace = 0.4f;

        chart = (BarChart) myView.findViewById(R.id.mealChart);
        chart.setDescription(null);
        chart.setPinchZoom(false);
        chart.setScaleEnabled(false);
        chart.setDrawBarShadow(false);
        chart.setDrawGridBackground(false);

        int groupCount = 6;

        ArrayList xVals = new ArrayList();

        xVals.add("Jan");
        xVals.add("Feb");
        xVals.add("Mar");
        xVals.add("Apr");
        xVals.add("May");
        xVals.add("Jun");

        ArrayList yVals1 = new ArrayList();
        ArrayList yVals2 = new ArrayList();

        yVals1.add(new BarEntry(1, (float) 1));
        yVals2.add(new BarEntry(1, (float) 2));
        /*yVals1.add(new BarEntry(2, (float) 3));
        yVals2.add(new BarEntry(2, (float) 4));
        yVals1.add(new BarEntry(3, (float) 5));
        yVals2.add(new BarEntry(3, (float) 6));
        yVals1.add(new BarEntry(4, (float) 7));
        yVals2.add(new BarEntry(4, (float) 8));
        yVals1.add(new BarEntry(5, (float) 9));
        yVals2.add(new BarEntry(5, (float) 10));
        yVals1.add(new BarEntry(6, (float) 11));
        yVals2.add(new BarEntry(6, (float) 12));
        */

        BarDataSet set1, set2;
        set1 = new BarDataSet(yVals1, "A");
        set1.setColor(Color.RED);
        set2 = new BarDataSet(yVals2, "B");
        set2.setColor(Color.BLUE);
        BarData data = new BarData(set1, set2);
        data.setValueFormatter(new LargeValueFormatter());
        chart.setData(data);
        chart.getBarData().setBarWidth(barWidth);
        chart.getXAxis().setAxisMinimum(0);
        chart.getXAxis().setAxisMaximum(0 + chart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
        chart.groupBars(0, groupSpace, barSpace);
        chart.getData().setHighlightEnabled(false);
        chart.invalidate();

        Legend l = chart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(true);
        l.setYOffset(20f);
        l.setXOffset(0f);
        l.setYEntrySpace(0f);
        l.setTextSize(8f);

        //X-axis
        XAxis xAxis = chart.getXAxis();
        xAxis.setGranularity(1f);
        xAxis.setGranularityEnabled(true);
        xAxis.setCenterAxisLabels(true);
        xAxis.setDrawGridLines(false);
        xAxis.setAxisMaximum(6);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setValueFormatter(new IndexAxisValueFormatter(xVals));
        //Y-axis
        chart.getAxisRight().setEnabled(false);
        YAxis leftAxis = chart.getAxisLeft();
        leftAxis.setValueFormatter(new LargeValueFormatter());
        leftAxis.setDrawGridLines(true);
        leftAxis.setSpaceTop(35f);
        leftAxis.setAxisMinimum(0f);

        return myView;
    }
}

个人资料(片段2)

public class Profile extends Fragment implements View.OnClickListener{
    View myView; //A view object called myView

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.profile, container, false);

        TextView txtBp = (TextView) myView.findViewById(R.id.viewBP);
        txtBp.setText(getProfileInformation("bp"));

        TextView txtCholesterol = (TextView) myView.findViewById(R.id.viewCholesterol);
        txtCholesterol.setText(getProfileInformation("cholesterol"));

        Button btn = (Button) myView.findViewById(R.id.btnVPDone);
        btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                FragmentManager fragmentManager = getFragmentManager();fragmentManager.beginTransaction()
                        .replace(R.id.content_frame
                                , new EditProfile())
                        .commit();
            }
        });
        return myView;
    }

    private String getProfileInformation(String information_name){
        Context context = getActivity();
        SharedPreferences sharedPref = context.getSharedPreferences(
                "user_profile", Context.MODE_PRIVATE);
        return sharedPref.getString(information_name,"");
    }

    @Override
    public void onClick(View view) {

    }
}

编辑个人资料(片段3)

public class EditProfile extends Fragment implements OnClickListener {
    View myView; //A view object called myView

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.editprofile, container, false);

        Button btn = (Button) myView.findViewById(R.id.btnEPDone);
        btn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
                EditText bp = (EditText) myView.findViewById(R.id.editBP);
                EditText cholesterol = (EditText) myView.findViewById(R.id.editCholesterol);
                if(bp.getText().toString().trim().length()>0)
                {
                    setProfileInformation(bp.getText().toString(), "bp");
                }
                else if(cholesterol.getText().toString().trim().length()>0)
                {
                    setProfileInformation(cholesterol.getText().toString(), "cholesterol");
                }
                FragmentManager fragmentManager = getFragmentManager();fragmentManager.beginTransaction()
                        .replace(R.id.content_frame
                                , new Profile())
                        .commit();
            }
        });
        return myView;
    }

    private void setProfileInformation(String info,String information_name){
        Context context = getActivity();
        SharedPreferences sharedPref = context.getSharedPreferences(
                "user_profile", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString(information_name, info);
        editor.apply();
    }

    @Override
    public void onClick(View view) {
        boolean checked = ((RadioButton) view).isChecked();
        switch(view.getId()) {

        }
    }
}

0 个答案:

没有答案