gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:35:26 25 4
gpt4 key购买 nike

我正在尝试将 SELECT 选项添加到我从休息服务中填充的微调器。我已经将项目的数量减一添加到适配器类中。如何将微调器提示作为最后一项添加到 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;

}
}

最佳答案

在创建此适配器的对象后,不要在主 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")
}

然后在你的 get count 中简单地这样做

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

并在获取 View 方法中

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

关于android - 如何将微调器提示作为最后一项添加到 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39263326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com