Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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

share|improve this question
2  
(Integer) getItem(getCount()), Why ? – Blackbelt Jul 19 at 10:53
1  
Try String.valueOf(getItem(getCount()) – damian Jul 19 at 10:54
    
@damian but why? getItem is already retuning a String – Blackbelt Jul 19 at 10:56
    
Ops, true, made my assumption too quickly. You're right, Integer casting is the culprit and String.valueOf not required. – damian Jul 19 at 11:00
    
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 at 11:18

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

share|improve this answer
    
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 at 11:14
    
What kind of object do you think it is returning ? – Abhinav Arora Jul 19 at 11:35
    
See the updated answer ! – Abhinav Arora Jul 19 at 11:46
    
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 at 12:47
up vote 0 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.