android应用程序中的多个订单条目

时间:2015-02-21 11:10:08

标签: android arrays

我正在研究Android的订单管理系统。 在用户提交订单的订单页面上,我希望它能够允许用户提交多个订单,然后将用户带到显示所有订单的页面。 我可以用什么方法来实现这一目标? 这是订单活动页面,由3个微调器和一个文本字段组成。此页面将用户带到ViewOrder页面,该页面显示用户选择的内容。而不是我希望用户首先提交多个订单,然后查看提交的所有订单的显示。任何帮助深表感谢。

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.example.androidhive.R;




public class OrderActivity extends Activity {
    public  Button btnOrder;
    public  EditText txtCrates;
    public Spinner sp1, sp2, sp3;
    public List<String> list;  
    public static String crates;
    public static final String EXTRA_MESSAGE = "com.example.myspinner.msg1";
    public static final String EXTRA_MESSAGE2 = "com.example.myspinner.msg2";
    public static final String EXTRA_MESSAGE3 = "com.example.myspinner.msg3";
    public static final String EXTRA_MESSAGE4 = "com.example.myspinner.MESSAGE";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order);

        sp1 = (Spinner) findViewById(R.id.spinner1);
        sp2 = (Spinner) findViewById(R.id.spinner2);
        sp3 = (Spinner) findViewById(R.id.spinner3);
        btnOrder = (Button) findViewById(R.id.btnOrder);
        txtCrates =(EditText) findViewById(R.id.crates);
        list = new ArrayList<String> ();
        list.add("CocaCola");
        list.add("Sprite");
        list.add("Fanta Orange");
        list.add("Fanta Pineapple");
        list.add("Fanta Blackcurrant");
        list.add("Fanta Passion");
        list.add("Krest");
        list.add("Stoney");
        list.add("Dasani");
        list.add("Minute Maid Apple");
        list.add("Minute Maid Mango");
        list.add("Minute Maid Orange");

        final String[] str1={"300ml","500ml","1 Litre","1.25 Litres","2 Litres"};

        ArrayAdapter<String> adp1 = new ArrayAdapter<String> 
        (this, android.R.layout.simple_dropdown_item_1line, list);

        ArrayAdapter<String> adp2 = new ArrayAdapter<String> 
        (this, android.R.layout.simple_dropdown_item_1line, str1);

        ArrayAdapter<CharSequence> adp3 = ArrayAdapter.createFromResource
                (this, R.array.str2, android.R.layout.simple_dropdown_item_1line);

        sp1.setAdapter(adp1);
        sp2.setAdapter(adp2);
        sp3.setAdapter(adp3);

        btnOrder.setOnClickListener(new View.OnClickListener() {
            /** Called when the user clicks the Submit Order button */
            public void onClick(View view) {

                    Intent intent = new Intent(getApplicationContext(),ViewOrder.class);

                      String msg1=sp1.getSelectedItem().toString();
                      String msg2=sp2.getSelectedItem().toString();
                      String msg3=sp3.getSelectedItem().toString();
                      String message = txtCrates.getText().toString();
                      intent.putExtra(EXTRA_MESSAGE, msg1);
                      intent.putExtra(EXTRA_MESSAGE2, msg2);
                      intent.putExtra(EXTRA_MESSAGE3, msg3);
                      intent.putExtra(EXTRA_MESSAGE4, message);
                      startActivity(intent);
            }
        });

        sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub

            Toast.makeText(getBaseContext(), list.get(arg2),
                Toast.LENGTH_SHORT).show();
        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

        sp2.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub

            Toast.makeText(getBaseContext(), str1[arg2],
                Toast.LENGTH_SHORT).show();
        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

        sp3.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub

            String item = sp3.getSelectedItem().toString();
            Toast.makeText(getBaseContext(), item, Toast.LENGTH_SHORT).show();
        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }


        });
        }

}

1 个答案:

答案 0 :(得分:0)

尝试以下操作后,为多个订单选择一个列表

ArrayList<String> orders=new ArrayList<String>(); 

作为活动中的类变量

然后将完整的订单存储为字符串,其中包含一些特殊字符作为分隔符,如

String msg1=sp1.getSelectedItem().toString();
String msg2=sp2.getSelectedItem().toString();
String msg3=sp3.getSelectedItem().toString();
String message = txtCrates.getText().toString();

orders.add(msg1+","+msg2+","+msg3+","+message);

然后为意图添加如下

intent.putStringArrayListExtra("orders", orders);

希望这会对你有所帮助。