android 从微调器获取文本

9rygscc1  于 2022-12-25  发布在  Android
关注(0)|答案(6)|浏览(227)

第一个月
所以我用这几行代码从spinner中获取文本,并把它传递到另一个java文件中,但是每当我这么做的时候,应用程序总是给我一个强制关闭。
如何解决此错误?
太感谢你了。

9lowa7mx

9lowa7mx1#

我希望这对你有帮助。
尝试此代码...
在这里,我将所选微调器的索引和值存储到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

    }

});

要将该值获取到另一个java文件中,请在该java文件中需要它的地方编写以下代码。

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

7qhs6swi2#

**尝试以下代码:**将spinnerName的适配器设置为:

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

现在将setOnItemSelectedListener设置为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

        }
    });
wr98u20j

wr98u20j3#

对不起,我发错了。请试试这个。

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("");
        }
});
kb5ga3dv

kb5ga3dv4#

请使用此选项:

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

66bbxpm55#

这应该行得通:

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

dfddblmv6#

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

相关问题