Having Multiple spinners in android
Can anyone please tell me how to add multiple spinners with only one onItemSelected(). Right now i am doing something like this: Spinner s[] = new Spin开发者_运维知识库ner[5]; I am using this for displaying hierarchy in spinners.
For index = 0; it works fine. But problem arises with next index as there is only one onItemSelected().
What can you suggest on this?
Note: I am coding to have spinners created dynamically and filled with corresponding cursor. Cursor is not an issue so far.
Thanks!
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);
}
}
}
精彩评论