如何将微调器提示作为最后一项添加到JSON响应中

时间:2016-09-01 05:28:32

标签: android json

我正在尝试将SELECT选项添加到spinner,我从rest服务填充。 我已将减少项目数减少一个添加到适配器类。 如何将微调器提示添加为JSON响应的最后一项。

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());

在适配器

   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;

}
}

2 个答案:

答案 0 :(得分:1)

spinnerArray.add("SELECT ONE");添加After for循环

答案 1 :(得分:1)

在创建此适配器的对象后,不要在主spinnerArray中添加该提示

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

在适配器数组SpinnerAdapter()构造函数中添加提示,如下所示

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

并且在你的计算中只需这样做

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

并在获取视图方法

@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;

}

然后这两行将完美地完成

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