gpt4 book ai didi

java - 使用微调器在 Activity 之间传输数据

转载 作者:行者123 更新时间:2023-12-01 15:06:17 25 4
gpt4 key购买 nike

我是 Android 开发新手。

我正在尝试将选定的数据从多个微调器(搜索 Activity )传输到另一个 Activity (JSON 搜索结果 Activity )

最后我有打开搜索结果的按钮

搜索 Activity :我有 java spinner

ArrayAdapter<CharSequence> whatlist = ArrayAdapter.createFromResource(this, 
R.array.whatlist, android.R.layout.simple_spinner_item);
whatlist.setDropDownViewResource(R.layout.spinner_style);
spwhat = (Spinner) findViewById(R.id.spWhat);
spwhat.setAdapter(whatlist);
spwhat.setOnItemSelectedListener(new MyOnItemSelectedListener());

和 MyOnItemSelectedListener

   public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

strs = new Bundle();
Intent i = new Intent(SearchActivity.this, SearchResult.class);
strs.putString("setwhat", parent.getItemAtPosition(pos).toString());
i.putExtras(strs);

}
public void onNothingSelected(AdapterView<?> arg0) {}
}

这是按钮

btnsearch = (Button)findViewById(R.id.btnSearch);
btnsearch.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

Intent ia = new Intent(SearchActivity.this, SearchResult.class);
SearchActivity.this.startActivity(ia);
}
});

这是在搜索结果中

  Bundle extras = getIntent().getExtras();
if(extras!=null){


Integer item = extras.getInt("setwhat");
//Use a switch(item) here to switch to different links based on selection
TextView tv = (TextView) findViewById(R.id.tvtv);
tv.setText("Another Activity, Item is :" + item.toString());

文本不会改变。我已经尝试过网络上的任何教程,并在这里搜索解决方案几个小时。有人可以帮忙吗?

最佳答案

你甚至不需要你的微调器的监听器。只需将按钮的 onclick 更改为:

btnsearch = (Button)findViewById(R.id.btnSearch); 
btnsearch.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

Spinner spwhat = (Spinner) findViewById(R.id.spWhat);

Intent ia = new Intent(SearchActivity.this, SearchResult.class);
ia.putExtra("setwhat", spwhat.getSelectedItem().toString());
startActivity(ia);
}
});

关于java - 使用微调器在 Activity 之间传输数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12904992/

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