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

时间:2014-10-23 11:12:56

标签: java android android-layout android-intent android-activity

我是android java编码的新手。我尝试做菜单列表,其中只有勾选框,一旦勾选项目,n按下,它应该去查看布局并显示项目和所选项目的总数,然后按下一步它应该打开详细信息用户必须输入详细信息的页面n按发送按钮通过电子邮件发送。我不知道如何将菜单从菜单调到购物车和确认类。

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

}




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

}




这是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>
&#13;
&#13;
&#13;

这是menu.xml

&#13;
&#13;
<?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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

让您想要在菜单类中获取的变量为static,然后在购物车类中,您可以像这样获取它们:

menu.variableFromMenuClass

答案 1 :(得分:-1)

 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.

}

这是你必须写的简单算法。