代码之家  ›  专栏  ›  技术社区  ›  CHAN TATA

Android Studio中的交互式微调器

  •  0
  • CHAN TATA  · 技术社区  · 6 年前

    我现在正在使用Android Studio开发一个移动应用程序。我想构建两个交互式微调器。SQLite数据库用于获取要显示的项目。我试图将第一个微调器的选定项存储在字符串中。如果第一个微调器中的选定项目=“1”,则第二个微调器会将适配器设置为显示相关项目。然而,结果表明交互失败。

        b_spinner.setEnabled(false);
    
        mAList =mDBHelper.getListA();
        a_adapter=new ListAAdapter(this, mAList);
    
        mBList =mDBHelper.getListB();
        b_adapter=new ListBAdapter(this, mBList);
    
        a_spinner.setAdapter(a_adapter);
        final String a_selected = ((Location)a_spinner.getSelectedItem()).getA();
        a_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(a_selected.equals("1")){
                    b_spinner.setEnabled(true);
                    b_spinner.setAdapter(b_adapter);
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    

    我是Android应用程序开发新手,我想问一下有没有更好的想法来构建多个交互式微调器。非常感谢。

    2 回复  |  直到 6 年前
        1
  •  0
  •   manivanna perumal    6 年前
    This will work :
    
    import java.util.ArrayList;
    import java.util.List;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Spinner;
    import android.widget.Toast;
    import android.widget.AdapterView.OnItemSelectedListener;
    
    class MainActivity extends Activity implements OnItemSelectedListener{
    
    
    
        Spinner spinner,spinner2;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Spinner element
             spinner = (Spinner) findViewById(R.id.spinner);
             spinner2 = (Spinner) findViewById(R.id.spinner2);
    
            // Spinner click listener
            spinner.setOnItemSelectedListener(this);
            spinner2.setOnItemSelectedListener(this);
    
            // Spinner Drop down elements
            List<String>  categories = new ArrayList<String>();
            categories.add("Automobile");
            categories.add("Business_Services");
            categories.add("Computers");
            categories.add("Education");
            categories.add("Personal");
            categories.add("Travel");
    
    
    
    
    
            // Creating adapter for spinner
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
    
            // Drop down layout style - list view with radio button
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
            // attaching data adapter to spinner
            spinner.setAdapter(dataAdapter);
        }
    
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            // On selecting a spinner item
            String item = parent.getItemAtPosition(position).toString();
    
            if(item.equals("Automobile")){
    
                List<String>  categories2 = new ArrayList<String>();
                categories2.add("1");
                categories2.add("2 Services");
                categories2.add("3");
                categories2.add("4");
                categories2.add("5");
                categories2.add("6");
                Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
                ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories2);
    
                // Drop down layout style - list view with radio button
                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
                // attaching data adapter to spinner
                spinner2.setAdapter(dataAdapter);
            }
            else if(item.equals("Business_Services")){
    
                List<String>  categories3 = new ArrayList<String>();
                categories3.add("11");
                categories3.add("22 Services");
                categories3.add("33");
                categories3.add("44");
                categories3.add("55");
                categories3.add("66");
                Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
                ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories3);
    
                // Drop down layout style - list view with radio button
                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
                // attaching data adapter to spinner
                spinner2.setAdapter(dataAdapter);
            }
    
            // Showing selected spinner item
    
        }
    
    
    
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    }
    

    和XML:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:padding="10dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            android:text="Category:"
            android:layout_marginBottom="5dp"/>
    
        <Spinner
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    
        <Spinner
            android:id="@+id/spinner2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
    
    </LinearLayout>
    
        2
  •  0
  •   J Ramesh    6 年前

    试试这个

         b_spinner.setEnabled(false);
            mAList =mDBHelper.getListA();
            a_adapter=new ListAAdapter(this, mAList);
    
            mBList =new ArrayList();
            b_adapter=new ListBAdapter(this, mBList);
            b_spinner.setAdapter(b_adapter);
            a_spinner.setAdapter(a_adapter);
            final String a_selected = ((Location)a_spinner.getSelectedItem()).getA();
            a_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    switch(position){
                       case 1:
                            b_spinner.setEnabled(true);
                            mBList.clear();
                            mBList.addAll(mDBHelper.getListB());
                            b_adapter.notifyDataSetChanged();
                        break;
                    }
                }
                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                }
            });