I have a problem with a custom adapter for put hint in spinner. I've made a class AdapterSpinnerhint that extends ArrayAdapter:
public class AdapterSpinnerHint extends ArrayAdapter {
int labelHint;
int textViewId;
int layout;
ArrayList<String> mItems;
Context context;
public AdapterSpinnerHint(Context context, int spinner_layout, int field, ArrayList<String> list, int label) {
super(context, spinner_layout, list);
textViewId = field;
labelHint = label;
layout = spinner_layout;
mItems=list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(layout, null);
if (position == getCount()) {
((TextView)v.findViewById(textViewId)).setText(labelHint);
((TextView)v.findViewById(textViewId)).setHint((Integer) getItem(getCount())); //"Hint to be displayed"
}
return v;
}
@Override
public int getCount() {
return mItems.size()-1; // you dont display last item. It is used as hint.
}
And in my activity I've created a function for Create spinner
private void createSpinnerCustomer(JSONArray customers) {
Spinner spinner = (Spinner) findViewById(R.id.customers_spinner);
ArrayList<String> customersList=new ArrayList<String>();
for(int i=0;i<customers.length();i++){
try {
customersList.add(customers.getJSONObject(i).getString("name"));
} catch (JSONException e) {
e.printStackTrace();
}
}
customersList.add(String.valueOf(R.string.customer_label));
assert spinner != null;
AdapterSpinnerHint adapter=new AdapterSpinnerHint(
getApplicationContext(), R.layout.spinner_layout, R.id.txt, customersList, R.string.customer_label);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner.setAdapter(adapter);
spinner.setSelection(adapter.getCount());
}
I would create a general adapter for all spinner that include hint. But I'm getting this error on AdapterSpinnerHint in this line:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
I've changed approach: in my activity:
private void createSpinnerCustomer(JSONArray customers) {
Spinner spinner = (Spinner) findViewById(R.id.customers_spinner);
AdapterSpinnerHint adapter2 = new AdapterSpinnerHint(ActivityActivity.this, android.R.layout.simple_spinner_dropdown_item, "Customers", customers, "name");
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
assert spinner != null;
spinner.setAdapter(adapter2);
spinner.setSelection(adapter2.getCount());
}
And my adapter:
public class AdapterSpinnerHint extends ArrayAdapter {
String labelHint;
JSONArray mItems;
String property;
public AdapterSpinnerHint(Context context, int spinner_layout, String label, JSONArray list, String getValue) {
super(context, spinner_layout);
labelHint = label;
mItems = list;
property = getValue;
addValue(mItems);
}
private void addValue(JSONArray customers) {
for(int i=0;i<customers.length();i++){
try {
this.add(customers.getJSONObject(i).getString(property));
} catch (JSONException e) {
e.printStackTrace();
}
}
this.add(labelHint);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
if (position == getCount()) {
((TextView)v.findViewById(android.R.id.text1)).setText("");
((TextView)v.findViewById(android.R.id.text1)).setHint((String) getItem(getCount())); //"Hint to be displayed"
}
return v;
}
@Override
public int getCount() {
return super.getCount()-1; // you dont display last item. It is used as hint.
}
}
And it work, but I've to put cast on setHint((String) getItem(getCount()) because I cant' compile app without.