如何将多个ArrayList <string>传递给另一个活动

时间:2017-04-10 20:49:12

标签: android android-intent

我尝试将三个数组传递给其他活动,但无论出于何种原因,似乎只有从父活动传递的最后一个数组传递给子活动,其余的不是 这是父活动代码:

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

    Intent intent = getIntent();
    categoryTitle = intent.getStringExtra(categoryName);

    if(Arrays.asList(FragmentHelper.Days).contains(categoryTitle)) {
        header.setText("Start the workout");
        addButton.setText("Start");
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context = v.getContext();
                Intent intent = new Intent(context, ItemDetailActivityCopy.class);
                Bundle extras = new Bundle();
                List<ListItem> items = listManager.getList(categoryTitle);
                ArrayList<String> itemName = new ArrayList<>();
                ArrayList<String> itemTime = new ArrayList<>();
                ArrayList<String> itemNumberOfSets = new ArrayList<>();

                for(int i = 0; i < listManager.getNumberOfItemsCopy(categoryTitle); i++) {
                    itemName.add(items.get(i).getDescription());
                    itemTime.add(String.valueOf(items.get(i).getTime()));
                    itemNumberOfSets.add(String.valueOf(items.get(i).getNumberOfSets()));
                }
                Log.d("Title:", itemName.toString());
                Log.d("Time:", itemTime.toString());
                Log.d("Num of sets:", itemNumberOfSets.toString());
                extras.putStringArrayList(ItemDetailActivityCopy.itemName.toString(), itemName);
                extras.putStringArrayList(ItemDetailActivityCopy.itemTime.toString(), itemTime);
                extras.putStringArrayList(ItemDetailActivityCopy.itemNumberOfSets.toString(), itemNumberOfSets);
                intent.putExtras(extras);
                context.startActivity(intent);
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

使用ArrayList<String>

ActivityActivity传递到其他putStringArrayListExtra
Intent intent = new Intent(context, ItemDetailActivityCopy.class);
intent.putStringArrayListExtra("LIST_ITEMS", itemName);
startActivity(intent);

从其他活动中获取ArrayList<String>

ArrayList<String> listItems;
listItems = getIntent().getStringArrayListExtra("LIST_ITEMS");