使用微调器在活动之间传输数据

时间:2012-10-15 22:36:10

标签: java android

我是Android开发的新手。

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

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

搜索活动: 我有java微调器

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());

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

1 个答案:

答案 0 :(得分:1)

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

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);
    }
});