问题
Fragment A
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 variableitemp=item;
,qtyp=qty;
,amtp=amt;
,totalp=total;
i.e,local variable data.Please help me out I'm extremly thankful to YOU all.Also P.S:-when I try to get data
itemp
,qtyp
.etc inonActivityCreated
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;
}
}
I'm calling the code like this:
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();
回答1:
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.
Fragment 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();
}
}
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);
}
});
}
来源:https://stackoverflow.com/questions/61868402/how-to-access-local-variable-by-storing-it-to-class-variableglobal-in-java-and