简体   繁体   中英

Transfer data between activity with spinners

i am new in Android development.

Im trying to transfer selected data from multiple spinners (Search activity) to another activity (JSON Search result activity)

and at the end i have button that open the search result

Search activity: i have the 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());

and the 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) {}
    }

This is the button

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

This is in the search result

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

the text wont change. i have tried any tutorial on the web and searching here for a solution for hours.. anyone can help?

You don't even need a listener for your spinner. Just change your button's onclick to this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM