代码之家  ›  专栏  ›  技术社区  ›  PK__

从2个微调器获取值并显示列表视图?

  •  -2
  • PK__  · 技术社区  · 10 年前

    场景是-用户从下拉列表中选择学期(sem1,sem2…等)值,从另一个下拉列表中的分支(cs,it…),根据这两个选择的值,列表视图包含指定学期和分支的科目。。

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.syllabus);
            String[] semarray = new String[] { "Sem 1", "Sem 2", "Sem 3" };
            String[] brancharray = new String[] { "CS", "EC", "EE" };
            semester = (Spinner) findViewById(R.id.sems);
            branch = (Spinner) findViewById(R.id.branch);
            semarrayAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, semarray);
            semester.setAdapter(semarrayAdapter);
            brancharrayAdapter = new ArrayAdapter<String>(
                    this, android.R.layout.simple_spinner_item, brancharray);
            branch.setAdapter(brancharrayAdapter);
    
    
            semester.setOnItemSelectedListener(new OnItemSelectedListener() {
    
                @Override
                public void onItemSelected(AdapterView<?> parent, View view,
                        int position, long id) {
                    item1 = semester.getSelectedItem().toString();
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    // TODO Auto-generated method stub
    
                }
            });
    
            branch.setOnItemSelectedListener(new OnItemSelectedListener() {
    
                @Override
                public void onItemSelected(AdapterView<?> parent, View view,
                        int position, long id) {
                    item2 = branch.getSelectedItem().toString();
    
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    // TODO Auto-generated method stub
    
                }
            });
    
            if(item1 == "Sem 1" && item2 == "CS"){
                Toast.makeText(this, "hi me hu",Toast.LENGTH_SHORT).show();
            };
    
    
        }
    

    如何实施???请提供帮助

    我对android很陌生

    1 回复  |  直到 10 年前
        1
  •  0
  •   Lennon Spirlandelli    10 年前

    试试这个:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.syllabus);
    
        String[] semarray = new String[] { "Sem 1", "Sem 2", "Sem 3" };
        String[] brancharray = new String[] { "CS", "EC", "EE" };
    
        semester = (Spinner) findViewById(R.id.sems);
        branch = (Spinner) findViewById(R.id.branch);        
        listView = (ListView) findViewById(R.id.listview);
    
        semarrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, semarray);
    
        semester.setAdapter(semarrayAdapter);
    
        brancharrayAdapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_spinner_item, brancharray);
    
        branch.setAdapter(brancharrayAdapter);
    
    
        semester.setOnItemSelectedListener(new OnItemSelectedListener() {
    
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                item1 = semester.getSelectedItem().toString();
    
                if(item1 != null && item2 != null){
    
                    String[] listArray = new String[]{item1 + " " + item2};
    
                    listViewAdapter = new ArrayAdapter<String>(this,
                            android.R.layout.simple_list_item_1, listArray);
    
                    listView.setAdapter(listViewAdapter);
    
                }
    
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
    
            }
    
        });
    
        branch.setOnItemSelectedListener(new OnItemSelectedListener() {
    
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
    
                item2 = branch.getSelectedItem().toString();
    
                if(item1 != null && item2 != null){
    
                    String[] listArray = new String[]{item1 + " " + item2};
    
                    listViewAdapter = new ArrayAdapter<String>(this,
                            android.R.layout.simple_list_item_1, listArray);
    
                    listView.setAdapter(listViewAdapter);
    
                }
    
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
    
            }
    
        });
    
        if(item1 == "Sem 1" && item2 == "CS"){
            Toast.makeText(this, "hi me hu",Toast.LENGTH_SHORT).show();
        };
    
    
    }