Reputation: 323
Below is my code. But it shows it is not possible. Can someone please suggest me how to set the arraylist to the spinner rather than setting simple array to the spinner.Below is my code.
ArrayList<String> categoryList = new ArrayList<String>();
//Here I have code to set string values to arraylist
//Below is the code where I am attempting to set the arraylist but it says "The constructor ArrayAdapter(new Runnable(){}, int, ArrayList) is undefined"
Spinner spinnerCategory = (Spinner)findViewById(R.id.spinnercategory);
ArrayAdapter<String> categoriesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categoryList);
Upvotes: 4
Views: 11032
Reputation: 7533
Use the following -
Spinner spinnerCategory = (Spinner)findViewById(R.id.spinnercategory);
ArrayAdapter<String> categoriesAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, categoryList);
Upvotes: 3
Reputation: 9217
Use customized adapter and implement it according to your data. It is just a sample not working code.
Spinner spinnerCategory = (Spinner)findViewById(R.id.spinnercategory);
spinnerCategory.setAdapter( new SpinnerAdapter() {
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return null;
}
});
Upvotes: 1
Reputation: 29199
Please use Context of activity as the first parameter of the ArrayAdapter, you can use
ActivityName.this
instead of this
, where the ActivityName is the name of the activity class. It seems you are running this code, in some Runnable, or Thread class, so right now, this
is the instance of a Runnable
object.
Upvotes: 3