从前一个微调器选择的项动态更改微调器中的字符串名称

时间:2016-08-22 17:10:28

标签: android spinner android-spinner

我想从先前的微调器选择项中更改spinner动态android中的字符串名称。

这是我的Activity类:

public class SelectionActivity extends AppCompatActivity implements View.OnClickListener {

    final String LOG = "Selection";
    private Spinner spBranch;
    private Spinner spSection;
    private Spinner spSemester,spSubject;
    private Button btnSend;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selection);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        spBranch = (Spinner) findViewById(R.id.spBranch1);
        spSection = (Spinner) findViewById(R.id.spSection1);
        spSemester = (Spinner) findViewById(R.id.spSemester1);
        spSubject=(Spinner) findViewById(R.id.spSubject1);
        String[] items1 = new String[]{"CSE", "EEE", "EE", "ECE", "MECH", "CIVIL"};
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items1);
        spBranch.setAdapter(adapter1);
        String[] items2 = new String[]{"A", "B", "C", "D", "E", "F"};
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items2);
        spSection.setAdapter(adapter2);
        String[] items3 = new String[]{"1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th"};
        ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items3);
        spSemester.setAdapter(adapter3);

        String[] items4 = new String[]{"Math 1", "Programming in C", "Thermodynamics", "Communication English", "Physics", "Basic Electronics"};
        String[] items5 = new String[]{"Chemistry", "Data Structure", "Mechanics", "Buiseness English", "Basic Electrical Engineering l ", "Math 2"};
        if(spSemester.getSelectedItem().toString().equals("1st"))
        {

            ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,items4);
            spSubject.setAdapter(adapter4);

        }
        else{

            ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,items5);
            spSubject.setAdapter(adapter4);

        }
        btnSend = (Button) findViewById(R.id.btnSend);

    } 



    @Override
    public void onClick(View v) {
        Intent in = new Intent(SelectionActivity.this, ListActivity.class);
        startActivity(in);
        HashMap postData = new HashMap();
        postData.put("txtBranch", spBranch.getSelectedItem().toString());
        postData.put("txtSection", spSection.getSelectedItem().toString());
        postData.put("txtSemester", spSemester.getSelectedItem().toString());

    }
}

我在此行spSubject.setAdapter(adapter4);收到错误 新的toorid,还有其他方法可以做到这一点

2 个答案:

答案 0 :(得分:0)

尝试使用此代码来了解是否选择了spSemester微调器中的第一个索引

JUTDMSTableAdapters.textBooksTableAdapter bookTableAdapter;
bookTableAdapter = new JUTDMSTableAdapters.textBooksTableAdapter();
JUTDMSTableAdapters.CourseTableAdapter courseTableAdapter;
courseTableAdapter = new JUTDMSTableAdapters.CourseTableAdapter();
courseTableAdapter.Insert( CourseID: txtCourseID.Text, CourseTitle: txtCourseTitle.Text);
bookTableAdapter.Insert( thirteenISBN: txt13ISBN.Text, CourseID: txtCourseID.Text, BookTitle: txtBookTitle.Text, Ancillary: txtAncillary.Text,
BookActive: txtBookActive.Text, ActiveDate: txtActiveDate.Text, InactiveDate: txtInactiveDate.Text, Author: txtAuthor.Text,
            Imprint: txtImprint.Text, Publisher: txtPublisher.Text, EditionAndDate: txtEditionDate.Text,
VendorISBN: vendISBN, tenISBN: txt10ISBN.Text, ebookAvailable: txtEBookAvailable.Text, eISBN: txtEISBN.Text, Notes: txtNotes.Text);

答案 1 :(得分:0)

请注意,您发布的所有代码都会在创建Activity时立即运行。这是onCreate()方法的重点。当用户在任何Spinner中选择新项时,不会发生任何事情。如果您希望Spinner更动态地更改,则需要添加适当的事件侦听器。特别是,您需要实施AdapterView.OnItemSelectedListener。显示了一个示例here。我建议创建单独的类来实现监听器接口,而不是在活动类上实现它。