尝试将从微调器选择的文本设置为textview时,应用程序崩溃

时间:2016-12-19 12:20:38

标签: android android-spinner

我想将所选项目中的文本从微调框设置为textview,但是当我尝试执行应用程序时。任何人都可以帮助我。

public class MainActivity extends AppCompatActivity {

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

        txt=(TextView) findViewById(R.id.txt1) ;

        ArrayList<ItemData> list=new ArrayList<>();
        list.add(new ItemData("Cash",R.drawable.paymentcashnew));
        list.add(new ItemData("Paytm",R.drawable.paymentpaytmnew));

        Spinner sp=(Spinner)findViewById(R.id.select_cash);
        String result = sp.getSelectedItem().toString();
        txt.setText(result);


        SpinnerAdapter adapter=new SpinnerAdapter(this,
                R.layout.spinner,R.id.txt,list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp.setAdapter(adapter);



    }
}

4 个答案:

答案 0 :(得分:0)

       sp.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    String result = sp.getSelectedItem().toString();
    txt.setText(result);
                }
            });

这可能有助于Frnd。

答案 1 :(得分:0)

spinner没有“getSelectedItem”。 https://developer.android.com/reference/android/widget/Spinner.html https://developer.android.com/guide/topics/ui/controls/spinner.html 以下是获取所选行的文本值的方法:https://stackoverflow.com/a/15545909/3022387

spin.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                String imc_met=spin.getSelectedItem().toString();

            }

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

            }
        });

答案 2 :(得分:0)

实施spinner.setOnItemClickListener,然后在此方法中查找文字。

spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
              String result = sp.getSelectedItem().toString();
              txt.setText(result);
            }
        });

答案 3 :(得分:0)

在设置适配器之前无法获取所选项目。请尝试以下代码

public class MainActivity extends AppCompatActivity {

final TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txt=(TextView) findViewById(R.id.txt1) ;

ArrayList<ItemData> list=new ArrayList<>();
list.add(new ItemData("Cash",R.drawable.paymentcashnew));
list.add(new ItemData("Paytm",R.drawable.paymentpaytmnew));

Spinner sp=(Spinner)findViewById(R.id.select_cash);
SpinnerAdapter adapter=new SpinnerAdapter(this,
        R.layout.spinner,R.id.txt,list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()     {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    Object item = parent.getItemAtPosition(position);
String result = sp.getSelectedItem().toString();
txt.setText(result);

}
public void onNothingSelected(AdapterView<?> parent) {
}
});


}
}