Fragmen A

Masalahnya di sini adalah setiap kali saya menelepon passitem(),passqty(),passamt(),passtot() saya mendapatkan nilai yang dideklarasikan pada saat inisialisasi.yaitu,int totalp = 0; Tali item = "", qtyp = "", amtp = "".Saya ingin variabel itemp=item;,qtyp=qty;,amtp=amt;,totalp=total;yaitu, lokal data variabel. Tolong bantu saya, saya sangat berterima kasih kepada ANDA semua.

Juga P.S:-ketika saya mencoba untuk mendapatkan data itemp,qtyp.etc di onActivityCreated saya mengerti dengan sempurna.

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;
    }

}

Saya memanggil kode seperti ini:

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();
0
APOORVA SINHA 18 Mei 2020, 14:18

1 menjawab

Jawaban Terbaik

Untuk mengatasi masalah ini, saya menambahkan metode getInstance ke Fragment_nonveg dan memanggilnya alih-alih membuat instance baru. Fragment_nonveg tidak langsung tersedia sebaliknya.

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

Aktivitas yang berisi Fragmen 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);
            }
        });

    }
1
thelr 27 Mei 2020, 08:37