简体   繁体   English

如何从另一个类调用对象并将其添加?

[英]How to call object from other class to another class and add it?

I'm new for android java coding. 我是android java编码的新手。 I'm try to do menu list where just have tick box, and once tick the items, n press next, it should go to view layout and show items and the total of the item selected, then press next it should open details page where user must put their details n press send button to send via email. 我尝试做一个菜单列表,其中只有复选框,然后勾选项目,然后按n,它应该去查看布局并显示项目和所选项目的总数,然后按下一步,它应该打开详细信息页面用户必须输入详细信息,然后按发送按钮以通过电子邮件发送。 I don't know how to call the items from menu to Cart and to confirmation class. 我不知道如何将菜单中的项目调用到购物车和确认类中。

This is menu.java . 这是menu.java。

 import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MenuActivity extends Activity { Button btnorder; Button btnback; Button btnlinkcart; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.menu); btnlinkcart = (Button) findViewById(R.id.button1); btnback = (Button) findViewById(R.id.button2); // back button click event btnback.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(MenuActivity.this, MainActivity.class); startActivity(intent); } }); // Link to Cart Screen btnlinkcart.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent i = new Intent(getApplicationContext(), ViewActivity.class); startActivity(i); finish(); } }); } } 

This is Cart.java 这是Cart.java

 import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class ViewActivity extends Activity { Button btnconfirm; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.cart); btnconfirm = (Button) findViewById(R.id.button1); // Link to Cart Screen btnconfirm.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent i = new Intent(getApplicationContext(), ConfirmActivity.class); startActivity(i); finish(); } }); } } 

This is cart.xml 这是cart.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/wallpaper" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:text="Confirm" /> </RelativeLayout> 

This is menu.xml 这是menu.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/wallpaper" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:text="Next" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:text="Back" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="28dp" android:text="Pizza (Large) RM30.00" android:textAppearance="?android:attr/textAppearanceLarge" /> <CheckBox android:id="@+id/checkBox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/checkBox1" android:layout_marginTop="20dp" android:text="Pizza (Mediume) RM20.00" android:textAppearance="?android:attr/textAppearanceLarge" /> <CheckBox android:id="@+id/checkBox3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/checkBox2" android:layout_marginTop="20dp" android:text="Pizza (Personal) RM10.00" android:textAppearance="?android:attr/textAppearanceLarge" /> <CheckBox android:id="@+id/checkBox4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/checkBox3" android:layout_marginTop="20dp" android:text="Chicken Wings RM12.00" android:textAppearance="?android:attr/textAppearanceLarge" /> <CheckBox android:id="@+id/checkBox5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/checkBox4" android:layout_marginTop="19dp" android:text="Garlic Bread RM6.00" android:textAppearance="?android:attr/textAppearanceLarge" /> <CheckBox android:id="@+id/checkBox6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/checkBox5" android:layout_marginTop="20dp" android:text="Soft Drink (Large) RM5.00" android:textAppearance="?android:attr/textAppearanceLarge" /> <CheckBox android:id="@+id/checkBox7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/checkBox6" android:layout_marginTop="19dp" android:text="Soft Drink (Medium) RM4.00" android:textAppearance="?android:attr/textAppearanceLarge" /> </RelativeLayout> 

Make the variable you want to get in the menu class static, then in the cart class you can get them like this: 将要在菜单类中获取的变量设为静态,然后在购物车类中获取如下所示的变量:

menu.variableFromMenuClass menu.variableFromMenuClass

 public static ArrayList<YourObject>  selectItemList= new ArrayList<YourObject>();


 on selectItem(){
 //each Item you add here 
 selectedList.add(selectedObject);
 }

 on disselectItem(){
//remove if exist in list
 selectedList.remove(disselectedOject);
 }


sendemail(){
 send your public static selecteditem list.

}

This is the algorithm you have to write simple. 这是您必须编写简单的算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM