开发者

Getting text from spinner

placeSpinner.getSelectedItem().toString();

so I'm using this coding lines to get the text from my sp开发者_如何学编程inner and pass it on to another java file. But the application keeps giving me a force close whenever I do this.

How can I resolve this error?

Thank you so much.


I hope it will helpful to you.

Try this Code...

Here i store the index and value of selected spinner into SharedPreferences

private SharedPreferences prefs;
private String prefName = "spinn";

final List<String> list=new ArrayList<String>();

list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
list.add("Item 4");
list.add("Item 5");

final Spinner sp1= (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adp1=new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,list);
adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adp1);

sp1.setOnItemSelectedListener(new OnItemSelectedListener()
{

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long id) {
    // TODO Auto-generated method stub
    int pos1=position;
    String str1=sp1.getSelectedItem().toString();

    prefs = getSharedPreferences(prefName, MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();

    //---save the values in the EditText view to preferences---
    editor.putInt("index", pos1);
    editor.putString("value", str1);

    //---saves the values---
    editor.commit();

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

    }

});

To get that value into another java file, inside that java file write below code where ever you need it..

        prefs = getSharedPreferences(prefName, MODE_PRIVATE);
        int pos=prefs.getInt("index", (Integer) null);
        String value=prefs.getString("value", "");


Try this code : set adapter to the spinnerName as:

spinnerName=(Spinner)findViewById(R.id.spinnerName);
    String []data={"item1","item2","item3","item4","item5"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, data);
    spinnerName.setAdapter(adapter);

Now set setOnItemSelectedListener to spinnerName.

spinnerName.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), spinnerName.getSelectedItem().toString(), Toast.LENGTH_LONG).show();


            Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
             //  pass value of selected spinnerItem to other Activity.
            intent.putExtra("spinnerItem", spinnerName.getSelectedItem().toString());

            startActivity(intent);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });


Sorry, I posted it wrong. Pls try this.

String []data={"item1","item2","item3","item4","item5"};

spinner.setAdapter(adaptador);
spinner.setOnItemSelectedListener(
    new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent,
            android.view.View v, int position, long id) {
                mData.setText("Selected: " + data[position]);
        }

        public void onNothingSelected(AdapterView<?> parent) {
            mData.setText("");
        }
});


Use this:

final String text = ((YourAdapter) spinner.getAdapter()).getItem(spinner.getSelectedItemPosition());


This should work:

@Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int position, long id) { 
    String value = (TextView)view.getText().toString();
    }


    final Equipamento[] eqptos = eqptoDao.list();

    List<String> labels = new ArrayList<String>();
    for(Equipamento eqpto : eqptos)
    {
        labels.add(eqpto.getDescricao());
    }

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item,labels);

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner.setAdapter(dataAdapter);

    //to get value of spinner

    eqptos[Integer.parseInt("" + spinner.getSelectedItemId())].getEquipamento()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜