如何通过将局部变量存储到java android中的类变量(全局)来访问局部变量

问题描述 投票:0回答:1

片段A

这里的问题是我打电话给我passitem()passqty()passamt()passtot()我得到的值是在初始化时声明,即int totalp = 0; String itemp = "", qtyp = "", amtp = ""。我想要变量itemp=item;qtyp=qty;amtp=amt;totalp=total;即本地可变数据。请帮助我,我非常感谢大家。

也请注意:-当我尝试获取数据itempqtyp .etconActivityCreated我的理解很完美。

public class Fragment_nonveg extends Fragment {


    TextView t,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;
    CheckBox c1,c2,c3,c4,c5;
    Button b1,b2,b5,b6,b7,b8,b9,b10,b11,b12;
    ImageButton b3;
    int x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,g1,g2,g3,g4,g5,g6,g7,g8,g9,g10;
    String res,res1,res2,res3,res4,re,re1,re2,re3,re4;
    //FloatingActionButton b4;
    int totalp = 0;
    String itemp = "", qtyp = "", amtp = "";

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
        int total = 0;
        String item = "", qty = "", amt = "";

                if (c1.isChecked() == true) {

                    item = item + c1.getText().toString() + "\n";
                    qty = qty + t2.getText().toString() + "\n";
                    amt = amt + t1.getText().toString() + "\n";
                    String t = t1.getText().toString();
                    total = total + Integer.parseInt(t);
                }
                if (c2.isChecked() == true) {
                    item = item + c2.getText().toString() + "\n";
                    qty = qty + t4.getText().toString() + "\n";
                    amt = amt + t3.getText().toString() + "\n";
                    String t = t3.getText().toString();
                    total = total + Integer.parseInt(t);
                }
                if (c3.isChecked() == true) {
                    item = item + c3.getText().toString() + "\n";
                    qty = qty + t6.getText().toString() + "\n";
                    amt = amt + t5.getText().toString() + "\n";
                    String t = t5.getText().toString();
                    total = total + Integer.parseInt(t);
                }
                if (c4.isChecked() == true) {
                    item = item + c4.getText().toString() + "\n";
                    qty = qty + t8.getText().toString() + "\n";
                    amt = amt + t7.getText().toString() + "\n";
                    String t = t7.getText().toString();
                    total = total + Integer.parseInt(t);

                }
                if (c5.isChecked() == true) {
                    item = item + c5.getText().toString() + "\n";
                    qty = qty + t10.getText().toString() + "\n";
                    amt = amt + t9.getText().toString() + "\n";
                    String t = t9.getText().toString();
                    total = total + Integer.parseInt(t);

                }
                itemp=item;
                qtyp=qty;
                amtp=amt;
                totalp=total;
 }
    public String passitem(){
        return itemp;
    }
    public String passqty(){
        return qtyp;
    }
    public String passamt(){
        return amtp;
    }
    public Integer passtot(){
        return totalp;
    }

}

我正在这样调用代码:

public class Menu extends AppCompatActivity {
  public FloatingActionButton b4;
  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);
    b4 = findViewById(R.id.fab);
    Fragment_nonveg fragment_n = new Fragment_nonveg();
    final String nitem=fragment_n.passitem();
    final String nqty=fragment_n.passqty();
    final String namt=fragment_n.passamt();
    final Integer ntotal=fragment_n.passtot();
java android fragment global local
1个回答
0
投票

片段A

public class Fragment_nonveg extends Fragment {


    TextView t,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;
    CheckBox c1,c2,c3,c4,c5;
    Button b1,b2,b5,b6,b7,b8,b9,b10,b11,b12;
    ImageButton b3;
    int x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,g1,g2,g3,g4,g5,g6,g7,g8,g9,g10;
    String res,res1,res2,res3,res4,re,re1,re2,re3,re4;
    //FloatingActionButton b4;
    int totalp = 0;
    String itemp = "", qtyp = "", amtp = "";

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
 //do your code
 public static Fragment_nonveg getInstance() {
        return instance;
    }
    public void calcu(){
        int total = 0;
        String item="",qty="",amt="";

        if (c1.isChecked() == true) {

            item=item+c1.getText().toString() + "\n";
            qty=qty+t2.getText().toString() + "\n";
            amt=amt+t1.getText().toString()+ "\n";
            String t = t1.getText().toString();
            total=total+Integer.parseInt(t);
        }
        if (c2.isChecked() == true) {
            item=item+c2.getText().toString()+"\n";
            qty=qty+t4.getText().toString()+"\n";
            amt=amt+t3.getText().toString() + "\n";
            String t = t3.getText().toString();
            total=total+Integer.parseInt(t);
        }
        if (c3.isChecked() == true) {
            item=item+c3.getText().toString() + "\n";
            qty=qty+t6.getText().toString() + "\n";
            amt=amt+t5.getText().toString() + "\n";
            String t = t5.getText().toString();
            total=total+Integer.parseInt(t);
        }
        if (c4.isChecked() == true) {
            item=item+c4.getText().toString() + "\n";
            qty=qty+t8.getText().toString() + "\n";
            amt=amt+t7.getText().toString()+ "\n";
            String t = t7.getText().toString();
            total=total+Integer.parseInt(t);

        }
        if (c5.isChecked() == true) {
            item = item + c5.getText().toString() + "\n";
            qty= qty + t10.getText().toString() + "\n";
            amt = amt+ t9.getText().toString() + "\n";
            String t = t9.getText().toString();
            total=total+Integer.parseInt(t);

        }
        itemp=item;
        qtyp=qty;
        amtp=amt;
        totalp=total;
        sp=this.getActivity().getSharedPreferences("calis", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("k1",item);
        editor.putString("k2",qtyp);
        editor.putString("k3", amtp);
        editor.putInt("k4", totalp);
        editor.commit();
    }
}

包含片段A的活动

public class Menu extends AppCompatActivity {

    TextView nonveg,veg,snacks,desert,beverges;
    ViewPager viewPager;
    PageViewAdapter pageViewAdapter;
    public FloatingActionButton b4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        b4 = findViewById(R.id.fab);
        b4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent obj4 = new Intent(Menu.this, Bill.class);
                Fragment_nonveg.getInstance().calcu();
                startActivity(obj4);
            }
        });

    }
© www.soinside.com 2019 - 2024. All rights reserved.