gpt4 book ai didi

android - 将列表传递给 Android 中的另一个 Activity

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:17:36 25 4
gpt4 key购买 nike

我已经创建了一个列表,并想将该列表传递给另一个 Activity ,但是当我创建 Intent 时,我在 putExtra 语句中遇到错误。只是想知道是否有任何简单的方法来传递字符串列表而不是单个字符串?

谢谢

private List<String> selItemList;
private ListView mainListView = null;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipes);
Button searchBtn = (Button) findViewById(R.id.searchButton);
searchBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (selItemList == null) {
Toast.makeText(getApplicationContext()," Please Make A Selection ", Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(Recipes2.this, XMLParser.class);
intent.putExtra("items_to_parse", selItemList);
startActivityForResult(intent, 0);
}
}
});

最佳答案

您可以使用 Intent 中的 putStringArrayListExtra

public Intent putStringArrayListExtra (String name, ArrayList value)

  private final List<String> selItemList;
private ListView mainListView = null;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipes);
Button searchBtn = (Button) findViewById(R.id.searchButton);
searchBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (selItemList == null) {
Toast.makeText(Recipes2.this," Please Make A Selection ", Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(Recipes2.this, XMLParser.class);
intent.putStringArrayListExtra("items_to_parse", (ArrayList<String>) selItemList);
startActivityForResult(intent, 0);
}
}
});

在你的 XMLParser.class 中:

 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (getIntent().getExtras() != null) {
for(String a : getIntent().getExtras().getStringArrayList("items_to_parse")) {
Log.d("=======","Data " + a);
}
}

关于android - 将列表传递给 Android 中的另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6087198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com