NothingSelectedSpinnerAdapter.java
package com.example.wastewarriorsnew;
public class NothingSelectedSpinnerAdapter extends Activity implements SpinnerAdapter, ListAdapter {
protected static final int EXTRA = 1;
protected SpinnerAdapter adapter;
protected Context context;
protected int nothingSelectedLayout;
protected int nothingSelectedDropdownLayout;
protected LayoutInflater layoutInflater;
public NothingSelectedSpinnerAdapter(
SpinnerAdapter spinnerAdapter,
int nothingSelectedLayout, Context context) {
this(spinnerAdapter, nothingSelectedLayout, -1, context);
}
public NothingSelectedSpinnerAdapter(SpinnerAdapter spinnerAdapter,
int nothingSelectedLayout, int nothingSelectedDropdownLayout, Context context) {
this.adapter = spinnerAdapter;
this.context = context;
this.nothingSelectedLayout = nothingSelectedLayout;
this.nothingSelectedDropdownLayout = nothingSelectedDropdownLayout;
layoutInflater = LayoutInflater.from(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nothing_selected_spinner_adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater()
.inflate(R.menu.nothing_selected_spinner_adapter, menu);
return true;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
int count = adapter.getCount();
return count == 0 ? 0 : count + EXTRA;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position == 0 ? null : adapter.getItem(position - EXTRA);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position >= EXTRA ? adapter.getItemId(position - EXTRA) : position - EXTRA;
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// This provides the View for the Selected Item in the Spinner, not
// the dropdown (unless dropdownView is not set).
if (position == 0) {
return getNothingSelectedView(parent);
}
return adapter.getView(position - EXTRA, null, parent); // Could re-use
// the convertView if possible.
}
protected View getNothingSelectedView(ViewGroup parent) {
return layoutInflater.inflate(nothingSelectedLayout, parent, false);
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 1;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return adapter.hasStableIds();
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return adapter.isEmpty();
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
adapter.registerDataSetObserver(observer);
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
adapter.unregisterDataSetObserver(observer);
}
@Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isEnabled(int position) {
// TODO Auto-generated method stub
return position != 0; // Don't allow the 'nothing selected'
// item to be picked.
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// Android BUG! http://code.google.com/p/android/issues/detail?id=17128 -
// Spinner does not support multiple view types
if (position == 0) {
return nothingSelectedDropdownLayout == -1 ?
new View(context) :
getNothingSelectedDropdownView(parent);
}
// Could re-use the convertView if possible, use setTag...
return adapter.getDropDownView(position - EXTRA, null, parent);
}
protected View getNothingSelectedDropdownView(ViewGroup parent) {
return layoutInflater.inflate(nothingSelectedDropdownLayout, parent, false);
}
}
这段代码我从这里开始。 我面临的问题是,一旦主要活动加载,就会显示第一个项目的2个Toast msgs。 当我选择第一个元素时,toast msg正在显示第二个元素。索引中的一些问题正在发生。 Plz帮助。
Thankss。
Mainactivity.java
String[] countries = {"Dehradun","Solapur","Pune","Mumbai"};
String[] city = {"Dehradun","Solapur","Pune","Mumbai"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Spinner element
final Spinner spinner = (Spinner) findViewById(R.id.spinner1);
final Spinner spinner1 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countries);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setAdapter(new NothingSelectedSpinnerAdapter(dataAdapter,
R.layout.contact_spinner_row_nothing_selected,this));
// R.layout.contact_spinner_nothing_selected_dropdown, // Optional
spinner.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, city);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter1);
spinner1.setAdapter(
new NothingSelectedSpinnerAdapter(
dataAdapter1,
R.layout.contact_spinner_row_nothing_selected,
// R.layout.contact_spinner_nothing_selected_dropdown, // Optional
this));
spinner1.setOnItemSelectedListener(this);
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
// On selecting a spinner item
Spinner spinner = (Spinner)parent;
Spinner spinner1 = (Spinner)parent;
if(spinner.getId() == R.id.spinner1)
{
Toast.makeText(this, "Selected: " +countries[position] , Toast.LENGTH_SHORT).show();
}
if(spinner1.getId() == R.id.spinner2)
{
Toast.makeText(this, "Selected: " + city[position], Toast.LENGTH_SHORT).show();
}
}