如何通过将局部变量存储到Java Android中的类变量(全局)来访问局部变量 [英] How to access local variable by storing it to class variable(global) in java android

查看:233
本文介绍了如何通过将局部变量存储到Java Android中的类变量(全局)来访问局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

片段A

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

The problem here is whenever I call passitem(),passqty(),passamt(),passtot() i get the value which is declared at the time of intialization.i.e,int totalp = 0; String itemp = "", qtyp = "", amtp = "".I want the variable itemp=item;,qtyp=qty;,amtp=amt;,totalp=total;i.e,local variable data.Please help me out I'm extremly thankful to YOU all.

也P.S:-当我尝试获取数据itempqtyp .etc时 onActivityCreated我感觉很好.

Also P.S:-when I try to get data itemp,qtyp.etc in onActivityCreated i'm getting it perfectly.

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();

推荐答案

为解决此问题,我在Fragment_nonveg中添加了getInstance方法,并调用了该方法,而不是构造了新实例. Fragment_nonveg否则无法直接使用.

To solve the problem, I added a getInstance method to Fragment_nonveg and called that instead of constructing a new instance. The Fragment_nonveg wasn't directly available otherwise.

片段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;
        public static int totalp;
    public static String itemp, qtyp, amtp;
    SharedPreferences sp;
    private static Fragment_nonveg instance;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Bundle bundle = this.getArguments();
        instance = this;
        return inflater.inflate(R.layout.fragment_nonveg, null);
    }


        @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的活动

Activity that contains Fragment 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);
            }
        });

    }

这篇关于如何通过将局部变量存储到Java Android中的类变量(全局)来访问局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆