我正在制作一个食物追踪应用程序,并希望让用户在1个活动中填写食物类别和食物项目,提交这些并在下一个活动中查看它们。他们应该能够提交多种食物,所有食物都应该出现在第二项活动中。但是,只有他们提交的最后一个才会出现在第二个活动中。
public class MainActivity extends ActionBarActivity {
EditText fooditem;
Spinner categorySpinner;
Button submitButton;
Button foodlistButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fooditem = (EditText) findViewById(R.id.fooditemEditText);
categorySpinner = (Spinner) findViewById(R.id.categorySpinner);
submitButton = (Button) findViewById(R.id.submitButton);
foodlistButton = (Button) findViewById(R.id.foodlistButton);
foodlistButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String stringCategory2 = categorySpinner.getSelectedItem().toString();
String stringFooditem2 = fooditem.getText().toString();
Intent intent = new Intent(getApplicationContext(),ListActivity.class);
intent.putExtra("category2", stringCategory2);
intent.putExtra("fooditem2", stringFooditem2);
//intent.putExtras(arrayFoodItems);
startActivity(intent);
}
});
这是我的活动二的代码:
public class ListActivity extends ActionBarActivity {
Button submitButton;
Button p2Button;
TextView p2TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
submitButton = (Button) findViewById(R.id.submitButton);
p2TextView = (TextView) findViewById(R.id.p2_foodlistTextView);
p2Button = (Button) findViewById(R.id.p2_button);
String category = getIntent().getStringExtra("category2");
String fooditem = getIntent().getStringExtra("fooditem2");
if(fooditem != null && !fooditem.isEmpty())
{
p2TextView.append(category + ": " + fooditem + "\n");
}
p2Button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
});
}
答案 0 :(得分:1)
创建字符串ArrayList
并在其上添加所有值,然后在提交按钮上点击将ArrayList
传递给另一个Activity
添加以下值:
List<String> category=new ArrayList<String>();
category.add("First");
category.add("Two");
然后将此ArrayList
作为附加内容传递给另一个Activity
并显示。