我为微调器创建了一个自定义适配器。微调器需要显示列表中的名称。以下是我的代码:
empNameList = new ArrayList<HashMap<String, String>>();
JSONArray jsonArray = response.getJSONArray("Salon");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String empName = jsonObject.getString("employeename");
String empId = jsonObject.getString("employee_id");
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("empId", empId);
hashMap.put("empName", empName);
empNameList.add(hashMap);
}
employeeAdapter = new EmployeeAdapter(getApplicationContext(), R.layout.service_list, empNameList);
employeeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(employeeAdapter);
这是我的Adapter类:
public class EmployeeAdapter extends ArrayAdapter<HashMap<String, String>> {
ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();
Context context;
int layoutResourceId;
HashMap<String, String> hashmap;
public EmployeeAdapter(Context context, int layoutResourceId, ArrayList<HashMap<String, String>> arrayList) {
super(context, layoutResourceId, arrayList);
this.arrayList = arrayList;
this.context = context;
this.layoutResourceId = layoutResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parrent) {
View view = convertView;
final ViewHolder holder;
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(layoutResourceId, parrent, false);
holder = new ViewHolder();
holder.textView42 = (TextView) view.findViewById(R.id.textView42);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
if (position % 2 == 0) {
view.setBackgroundColor(context.getResources().getColor(R.color.pink));
} else {
view.setBackgroundColor(Color.WHITE);
}
hashmap = new HashMap<String, String>();
hashmap = arrayList.get(position);
System.out.println("empName = " + hashmap.get("empName"));
holder.textView42.setText(hashmap.get("empName"));
return view;
}
static class ViewHolder {
TextView textView42;
}
Spinner正在显示hashmap,而不是empNameList的名称。任何帮助都将不胜感激。
答案 0 :(得分:1)
只需覆盖适配器类中的以下方法。
public View getDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(layoutResourceId, parent, false);
// View row = inflater.inflate(yourRowlayout, parent,false);
TextView make = (TextView) view.findViewById(R.id.textviewname);
view.setBackgroundColor(Color.GRAY);
make.setText(arrayList.get(position).get("empName"));
return view;
}