将按钮存储到列表中并在另一个活动中查看它们?

时间:2017-04-22 16:10:32

标签: android arrays list android-studio button

我是android studio的新手,我正在尝试自学基础知识。我目前有各种食品(沙拉酱)的按钮。一旦按下,我希望食物项目存储在一个列表中,这样一旦我进入下一个活动,我可以在那里显示它,显示被按下的项目。

SaladDressing.java:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SaladDressing extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_salad_dressing);

    Button button1 = (Button) findViewById(R.id.BalsamicVinaigrette);
    button1.setText("Balsamic Vinaigrette");

    Button button2 = (Button) findViewById(R.id.Caesar);
    button2.setText("Caesar");

    Button button3 = (Button) findViewById(R.id.BleuCheese);
    button3.setText("Bleu Cheese");

    Button button4 = (Button) findViewById(R.id.HM);
    button4.setText("Honey Mustard");

    Button button5 = (Button) findViewById(R.id.LoCalItalian);
    button5.setText("Lo-Cal Italian");

    Button button6 = (Button) findViewById(R.id.CM);
    button6.setText("Chipotle Mayo");

    Button button7 = (Button) findViewById(R.id.Ranch);
    button7.setText("Ranch");

    Button button8 = (Button) findViewById(R.id.ThousandIsland);
    button8.setText("Thousand Island");

    button1.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            startActivity(new Intent(SaladDressing.this, Orderlist.class));
        }
    });

    button2.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            startActivity(new Intent(SaladDressing.this, Orderlist.class));
        }
    });

    button3.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            startActivity(new Intent(SaladDressing.this, Orderlist.class));
        }
    });

    button4.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            startActivity(new Intent(SaladDressing.this, Orderlist.class));
        }
    });

    button5.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            startActivity(new Intent(SaladDressing.this, Orderlist.class));
        }
    });

    button6.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            startActivity(new Intent(SaladDressing.this, Orderlist.class));
        }
    });

    button7.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            startActivity(new Intent(SaladDressing.this, Orderlist.class));
        }
    });
    button8.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            startActivity(new Intent(SaladDressing.this, Orderlist.class));
        }
    });




}
}

然而,我不确定如何保存这些印刷机并将它们显示在Orderlist.java和布局文件中。我想我必须使用ArrayList,但我不知道如何使用它来存储按钮按下。

谢谢!

1 个答案:

答案 0 :(得分:0)

如果您想允许多项选择,我建议您使用CheckBoxes代替Buttons。此外,您应该使用SaladDressing扩展View.OnClickListener课程并实施void onClick(View v)。然后,不是为每个视图点击监听器设置多个匿名类,而是只有一个,并将其设置为所有视图,如button1.setOnClickListener(this);,依此类推。 OnClickListener方法实现可能如下所示:

public void onClick(View v) {
            String dressing = ((Button) v).getText();
            Intent intent = new Intent(SaladDressing.this, Orderlist.class);
            intent.putExtra("selected_dressing", dressing);
            startActivity(intent);
        }