Android Multi Spinner

时间:2012-08-03 11:56:42

标签: android android-spinner

我想开发一个带有三个微调器的Android应用程序。 这三个微调器将包含有关汽车的信息,包括品牌,型号和引擎。

  1. 因此,在选择第一个微调器(品牌)后,应根据所选品牌添加第二个微调器(型号)的内容。
  2. 接下来,将选择第二个微调器,然后添加第三个微调器(引擎)。
  3. 我在google和stackoverflow上搜索了关键字“Android Multi Spinners”和“针对微调器的Android监听器”近一周,但我仍然没有找到解决方案。

      

    以下是我对我的代码提出想法的链接

         

    所以,我有两个版本的源代码,但是它们都没有用。 (编辑:现在工作)

    对于第一个版本

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.Spinner;
    
    public class AddCarActivity extends Activity {
    
        private Spinner spnBrand, spnModel, spnEngine;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.add_car);
    
            addItemsOnSpnBrand();   
        }
    
        public void addItemsOnSpnBrand() {
            spnBrand = (Spinner) findViewById(R.id.spnBrand);
            List<String> list = new ArrayList<String>();
    
            //Get Brand Database and Add to List
    
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spnBrand.setAdapter(dataAdapter);       
    
            addListenerOnSpnBrandItemSelection();
        }
    
        public void addListenerOnSpnBrandItemSelection() {
            spnBrand = (Spinner) findViewById(R.id.spnBrand);       
            spnBrand.setOnItemSelectedListener(new OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                    addItemOnSpnModel(parent.getItemAtPosition(pos).toString());
                }
    
                public void onNothingSelected(AdapterView<?> parent) {
                    return;
                }
            });
        }
    
        public void addItemOnSpnModel(String inBrand) {
            spnModel = (Spinner) findViewById(R.id.spnModel);
            List<String> list = new ArrayList<String>();
    
            //Get Model Database and Add to List
    
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spnSeries.setAdapter(dataAdapter);      
    
            addListenerOnSpnModelItemSelection();
        }
    
        public void addListenerOnSpnModelItemSelection() {
            spnModel = (Spinner) findViewById(R.id.spnModel);
            spnModel.setOnItemSelectedListener(new OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                    addItemOnSpnEngine(parent.getItemAtPosition(pos).toString());               
                }
    
                public void onNothingSelected(AdapterView<?> parent) {
                    return;
                }
            });
        }
    
        public void addItemOnSpnEngine(String inModel) {
            spnEngine = (Spinner) findViewById(R.id.spnEngine);
            List<String> list = new ArrayList<String>();
    
            //Get Engine Database and Add to List
    
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spnEngine.setAdapter(dataAdapter);
        }
    }
    

    这是我的第二个版本

    public class AddCarActivity extends Activity implements OnItemSelectedListener {
    
        private Spinner spnBrand, spnModel, spnEngine;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.add_car);
    
            addItemsOnSpnBrand();
        }
    
        public void onItemSelected(AdapterView<?> parent, View view, int position,long arg3) {
            int id = parent.getId();
            switch (id) 
            {
                case R.id.spnBrand:
                    addItemOnSpnModel(parent.getItemAtPosition(position).toString()); break;
                case R.id.spnModel:
                    addItemOnSpnEngine(parent.getItemAtPosition(position).toString()); break;
            }
        }
    
        public void onNothingSelected(AdapterView<?> arg0) {
            return;
        }
    
        public void addItemsOnSpnBrand() {
            spnBrand = (Spinner) findViewById(R.id.spnBrand);
            List<String> list = new ArrayList<String>();
    
            //Get Brand Database and Add to List
    
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spnBrand.setAdapter(dataAdapter);
    
            spnBrand.setOnItemSelectedListener(this);
        }
    
        public void addItemOnSpnModel(String inBrand) {
            spnModel = (Spinner) findViewById(R.id.spnModel);
            List<String> list = new ArrayList<String>();
    
            //Get Model Database and Add to List
    
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spnModel.setAdapter(dataAdapter);
    
            spnModel.setOnItemSelectedListener(this);
        }
    
        public void addItemOnSpnEngine(String inModel) {
            spnEngine = (Spinner) findViewById(R.id.spnEngine);
            List<String> list = new ArrayList<String>();
    
            //Get Engine Database and Add to List
    
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spnEngine.setAdapter(dataAdapter);
        }
    }
    

    谢谢大家试图帮助我的人。我刚刚在重新测试用于回答Hip Hip阵列的代码时发现了这个问题。我在addListenerOnSpnModelItemSelection()中使用了错误的变量。我错误地使用spnBrand而不是spnModel。现在,这两个版本都有用,所以我纠正了它们,并希望它们能帮助那些尝试在android中使用多个微调器的人。

3 个答案:

答案 0 :(得分:1)

有点啰嗦,但试一试

Public class HomePage extends Activity implements OnClickListener, OnItemSelectedListener{

int orientation;
DataBaseHelper myDbHelper;
Cursor c3, c2;
SimpleCursorAdapter adapterData;
int index = 0;
Spinner s[] = new Spinner[5];
TableRow row;

@SuppressWarnings("static-access")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myDbHelper = new DataBaseHelper(this);

//-----------Customized Adapter for Specialties----Database binded//
String[] columns2 =  {myDbHelper.KEY_ID, myDbHelper.KEY_TITLE};
String table2 = myDbHelper.DB_TABLE_NAME;
c2 = myDbHelper.getHandle().query(table2, columns2, "type = 'SuperTab'", null, myDbHelper.KEY_TITLE, null, myDbHelper.KEY_TITLE);
startManagingCursor(c2);
String[] from = new String[]{myDbHelper.KEY_TITLE};
int[] to = new int[]{android.R.id.text1};
adapterData =
      new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c2, from, to );
adapterData.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );


//-----------Customized Adapter for Location-----Database binded--//
    String[] columns3 =  {myDbHelper.KEY_ID, myDbHelper.KEY_LOCATION};
    String table3 = myDbHelper.DB_TABLE_NAME_LOCATION;
    c3 = myDbHelper.getHandle().query(table3, columns3, null, null, myDbHelper.KEY_LOCATION, null, null);
    startManagingCursor(c3);
    String[] from3 = new String[]{myDbHelper.KEY_LOCATION};
    int[] to3 = new int[]{android.R.id.text1};
    SimpleCursorAdapter adapterData3 =
          new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c3, from3, to3 );
        adapterData3.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
//--------------------------------------------------------------//

Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
        this, R.array.specialty_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(adapterData);      

Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
adapter = ArrayAdapter.createFromResource(
        this, R.array.office_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapterData3);

Button search = (Button)this.findViewById(R.id.Search);
search.setOnClickListener(this);

Button searchNearMe = (Button)this.findViewById(R.id.Near_Me);
searchNearMe.setOnClickListener(this);

spinner.setOnItemSelectedListener(this);  

orientation = getResources().getConfiguration().orientation;

s[index] =  new Spinner(this);
// s[index].setOnItemSelectedListener(this);
row = new TableRow(this);
}

@SuppressWarnings("static-access")

public void onItemSelected(AdapterView<?> parent, View view, int pos,
    long id) {
TextView tv = (TextView)this.findViewById(R.id.spinnerText2);   


Log.w("ID: ", "index=" + adapterData.getItemId(pos) + " " + c2.getCount() + " " + id + " callee " + view);

String sql = "sql" +                     id                                                                 ;

c2 = myDbHelper.getHandle().rawQuery(sql, null);
Log.w("ID Adapter: ", "index=" + adapterData.getItemId(pos) + " " + c2.getCount());

int thisOrientation = getResources().getConfiguration().orientation;

if (thisOrientation != orientation) {
    return;
}       

//--------sets hierarchy spinner to corresponding database-----//
String[] from = new String[]{myDbHelper.KEY_TITLE};
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter adapterDataNew =
      new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c2, from, to );
adapterDataNew.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
Log.w("Adapter Data New", "index=" + adapterDataNew.getItemId(pos) + " " + adapterDataNew.getCount());
//------------------------------------------------------------//

//-----------------Table Row for Landscape mode---------------//

TableLayout tl = (TableLayout)this.findViewById (R.id.Table);
TableLayout.LayoutParams tlp = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
tlp.weight = 1;
tlp.setMargins(20, 15, 20, 0);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));       

//------------------------------------------------------------//

//------------------find Relative Layout by ID----------------//
RelativeLayout relative = (RelativeLayout)this.findViewById(R.id.rlay);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
        android.view.ViewGroup.LayoutParams.FILL_PARENT, 
        android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, R.id.spinner);
p.setMargins(15, 5, 15, 5);
//------------------------------------------------------------//

if (orientation != getResources().getConfiguration().ORIENTATION_LANDSCAPE) {
    if (c2.getCount() > 0) {                
        relative.removeView(s[index]);
        relative.addView(s[index], android.view.ViewGroup.LayoutParams.MATCH_PARENT, 
                android.view.ViewGroup.LayoutParams.MATCH_PARENT);
        s[index].setVisibility(view.VISIBLE);
        s[index].setPromptId(R.string.Sub);
        s[index].setAdapter(adapterDataNew);
        s[index].setLayoutParams(p);
        tv.setPadding(10, 50, 10, 0);

        Log.w("Index value ", " " + index);

    }
    else {
        s[index].setVisibility(view.GONE);
        relative.removeView(s[index]);
        tv.setPadding(0, 0, 0, 0);
    }
}
else {
        if (c2.getCount() > 0) {
            tl.removeView(row); 
            row.removeView(s[index]);                   
            Log.w("Row Parent " + row.getParent()," tl child " + tl.getChildCount());
            tl.addView(row, index + 4 , tlp);
            row.addView(s[index]);
            Log.w("Row Parent After Add" + row.getParent()," tl child After Add" + tl.getChildCount());
            row.setVisibility(view.VISIBLE);                    
            s[index].setVisibility(view.VISIBLE);
            s[index].setPromptId(R.string.Sub);
            s[index].setAdapter(adapterDataNew);

            Log.w("ID: Landscape mode", "index=" + adapterDataNew.getItemId(pos) + " " + adapterDataNew.getCount());
        }   
        else {
            s[index].setVisibility(view.GONE);
            row.setVisibility(view.GONE);
            row.removeView(s[index]);
            tl.removeView(row);                 
        }

}

}


public void onNothingSelected(AdapterView<?> arg0) {        

}

@Override
public void onClick(View v) {
EditText et1 = (EditText)this.findViewById(R.id.entry);
EditText et2 = (EditText)this.findViewById(R.id.entry2);
String lastName = et1.getText().toString();
String firstName = et2.getText().toString();
if (lastName.length() == 0) {
    lastName = "%";
}
if (firstName.length() == 0) {
    firstName = "%";
}
Bundle bundle = new Bundle();
bundle.putString("LastName", lastName);
bundle.putString("FirstName", firstName);
switch(v.getId()) {
    case R.id.Search:
        Intent i = new  Intent(this, Search.class);
        i.putExtras(bundle);
        startActivity(i);
        c2.close();
        c3.close();
        break;
    case R.id.Near_Me:
        Intent j = new  Intent(this, SearchNearMe.class);
        startActivity(j);

}       
}
}

答案 1 :(得分:0)

可以说,你有3个微调器spinner_1,spinner_2和spinner_3然后:

spinner_1.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {

                            if(spinner_1.getSelectedItem().equals("Spin1-Item-1")){
                            {
                                  // set adapter to spinner_2 here for "Spin1-Item-1" selected
                            }
                            else if(spinner_1.getSelectedItem().equals("Spin1-Item-2"))
                            {
                                 // set adapter to spinner_2 for "Spin1-Item-2" not selected
                            }
                            else 
                            {
                                 // set adapter to spinner_2 for "Spin1-Item-3" not selected
                            }
            }    
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {                
            }
});
spinner_2.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {

                            if(spinner_2.getSelectedItem().equals("Spin2-Item-1")){
                            {
                                  // set adapter to spinner_3 here for "Spin2-Item-1" selected
                            }
                            else if(spinner_2.getSelectedItem().equals("Spin2-Item-2"))
                            {
                                 // set adapter to spinner_3 for "Spin2-Item-2" not selected
                            }
                            else 
                            {
                                 // set adapter to spinner_3 for "Spin2-Item-3" not selected
                            }
            }    
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {                
            }
});

答案 2 :(得分:0)

是的,说明了Android多级微调器的实现和用法,当用户单击n第一微调器时,将根据第一微调器值加载第二微调器。

enter image description here

http://www.androidcoding.in/2020/06/08/android-multilevel-spinner/