-1

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

LorenzoBerti
  • 6,704
  • 8
  • 47
  • 89

2 Answers2

0

In your function - getView see this line - .setHint((Integer)getItem(getCount()));

.setHint(string) requires string as a parameter and you are passing an Integer by parsing a string as an Integer.

do like this

.setHint(getItem(getCount));

When you are calling super in the constructor of ArrayAdapter and passing the list of strings as a parameter, basically you are telling the ArrayAdapter that they type of objects that will be in the list will be String. So when you do getItem(), it will be returning a String

Abhinav Arora
  • 537
  • 1
  • 7
  • 23
  • thank you, I'm new in Android programming, and certainly I'm doing something wrong, but if no septum casting can not fill because getItem (getCount ())) gives me an object. but obviously I'm wrong somewhere else then. – LorenzoBerti Jul 19 '16 at 11:14
  • What kind of object do you think it is returning ? – Abhinav Arora Jul 19 '16 at 11:35
  • ok I understand thank you, so I've some problem, in another part of application because if I don't cast I can't compile, and if i put String and I click on spinner i receive rrayAdapter requires the resource ID to be a TextView However thankyou for explanation – LorenzoBerti Jul 19 '16 at 12:47
0

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.

LorenzoBerti
  • 6,704
  • 8
  • 47
  • 89