0

我是 Android 开发的新手。

我试图将选定的数据从多个微调器(搜索活动)传输到另一个活动(JSON 搜索结果活动)

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

搜索活动:我有 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());

文字不会改变。我已经尝试了网络上的任何教程并在这里搜索了几个小时的解决方案.. 任何人都可以提供帮助吗?

4

1 回答 1

1

你甚至不需要你的微调器的监听器。只需将按钮的 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);
    }
});
于 2012-10-15T23:11:14.037 回答