将微调器值传递给按钮单击触发器上的另一个活动

时间:2017-01-10 13:00:35

标签: java android

我在这里有一个简单的问题,但我仍然不知道该怎么做。所以,我的类有一个带有EditText值的Spinner,它将传递给另一个活动。我能够通过按钮单击触发器将EditText传递给另一个活动。但我不知道如何传递我在String中设置的Spinner值,如下面的代码。也许,有人可以告诉我如何在单击按钮时将两个值都传递给下一个活动。我希望有更好的解决方案。谢谢。

public class SearchActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText editTextProduct;
    private Button btnSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_product);

        editTextProduct = (EditText) findViewById(R.id.editTextProduct);
        btnSearch = (Button) findViewById(R.id.btnSearch);

        btnSearch.setOnClickListener(this);

        initializedSpinner();
    }

    private void initializedSpinner(){
        // Spinner element
        Spinner spinner = (Spinner) findViewById(R.id.spinner_search);

        // Spinner Drop down elements
        List<String> categories = new ArrayList<String>();
        categories.add("Search By Name");
        categories.add("Search By Type");

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);

        //spinner.setAdapter(dataAdapter);
        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinner.setAdapter(dataAdapter);
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                String searchItem = parent.getItemAtPosition(position).toString();

                Toast.makeText(parent.getContext(),searchItem + " Selected" , Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void searchData() {
        String productName = editTextProduct.getText().toString().trim();
        if (productName.equals("")) {
            Toast.makeText(this, "Please Enter Product Name", Toast.LENGTH_LONG).show();
            return;
        }
        else{
            Intent i = new Intent(SearchActivity.this, ListProductActivity.class);
            EditText tv5 = (EditText) findViewById(R.id.editTextProduct);
            i.putExtra("productName",tv5.getText().toString());
            startActivity(i);
        }
    }

    @Override
    public void onClick(View v) {

        searchData();
    }
}

3 个答案:

答案 0 :(得分:1)

在微调器点击上尝试此操作:

String Text = mySpinner.getSelectedItem().toString();

您将获得在微调器中选择的字符串。 无论你得到什么文本值,你只需要通过意图传递putExtras。

答案 1 :(得分:0)

您可以传递如下值:

@Override
    public void onClick(View v) {

      Intent i = new Intent(SearchActivity.this, ListProductActivity.class);
            i.putExtra("editTextValue",editTextProduct.getText().toString());
            i.putExtra("spinnerValue",spinner.getSelectedItem().toString());
            startActivity(i);
    }

答案 2 :(得分:0)

String[] stdarray = { "KG", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "Other" };

       ArrayAdapter aa = new ArrayAdapter<String>(getActivity(), R.layout.layout_std, stdarray);

       final ListView lv = (ListView) rootView.findViewById(R.id.listView);

       lv.setAdapter(aa);

       lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               Tab1.sentfromStd(lv.getItemAtPosition(position).toString()); //I used static method here sentfromStd(), You can also use intents like others said for getting your job done.
           }
       });