user3785322 user3785322 - 3 months ago 15
JSON Question

How to add spinner hint as last item to JSON response

I am trying to add SELECT option to spinner which i populate from rest service.
I have added the decrease the count of item by one to the adapter class.
How to add spinner hint as last item to JSON response.

public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
ArrayList<String> spinnerArray = new ArrayList<String>();

for (int i = 0; i < response.length(); i++) {
try {
JSONObject c = response.getJSONObject(i);
String employeeIdArray = c.getString("employeeId");

spinnerArray.add("SELECT ONE");

spinnerArray.add(employeeIdArray);
} catch (JSONException e) {
e.printStackTrace();
}
}

final SpinnerAdapter spinnerAdapter = new SpinnerAdapter(MainActivity.this, spinnerArray);

spinner.setAdapter(spinnerAdapter);
spinner.setSelection(adapter.getCount());


In adapter

public class SpinnerAdapter extends ArrayAdapter<String> {

public SpinnerAdapter(Context context, ArrayList<String> employeeIds) {
super(context, R.layout.spinner_id, employeeIds);
}

@Override
public int getCount() {
// don't display last item. It is used as hint.
int count = super.getCount();
return count > 0 ? count - 1 : count;
}

private static class ViewHolder {
TextView employeeId;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
String employeeIds = getItem(position);
ViewHolder viewHolder;

if (convertView == null) {
viewHolder = new ViewHolder();

LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.spinner_id, parent, false);

viewHolder.employeeId = (TextView) convertView.findViewById(R.id.value_employee_employeeId);

convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.employeeId.setText(employeeIds);

return convertView;

}
}

Answer

Don't add that hint in main spinnerArray, after creating object of this adapter

final SpinnerAdapter spinnerAdapter = new SpinnerAdapter(MainActivity.this, spinnerArray);

add hint in adapter array SpinnerAdapter() constructor like this

public SpinnerAdapter(Context context, ArrayList<String> employeeIds) {
    super(context, R.layout.spinner_id, employeeIds);
    employeeIds.add("SELECT ONE")
}

and in your get count simply do this

@Override
public int getCount() {
    // don't display last item. It is used as hint.
    int count = super.getCount();
    return count > 0 ? count - 1 : count;
}

and in get view method

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    String employeeIds = getItem(position);
    ViewHolder viewHolder;

    if (convertView == null) {
        viewHolder = new ViewHolder();

        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.spinner_id, parent, false);

        viewHolder.employeeId = (TextView) convertView.findViewById(R.id.value_employee_employeeId);

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    if (position == getCount()) {
        viewHolder.employeeId.setText("");
        viewHolder.employeeId.setHint(employeeIds); //"Hint to be displayed"
    } else {
        viewHolder.employeeId.setText(employeeIds);
    }
    return convertView;

}

and then this two lines will wok perfectly

spinner.setAdapter(spinnerAdapter);
spinner.setSelection(adapter.getCount());