我正在尝试使用autocompletetextview向用户显示员工列表。我还想从列表中单击的列表项中获取员工ID。但是,尽管我的onResponse()方法使用截击返回了期望的结果,但我并未与列表一起显示。
我尝试初始化ArrayAdapter并使用模型Employee类。有人可以帮我吗?
public void onResponse( String response ) {
try {
Employee employee = null;
List<Employee> employeeResponseList = new ArrayList<Employee>();
JSONObject rootObject = new JSONObject(response);
JSONArray employeeRow = rootObject.getJSONArray("payload");
Log.d(TAG, "onResponse: Response = " + response);
for ( int i = 0; i < employeeRow.length(); i++ ) {
String employeeName = employeeRow.getJSONObject(i).getString("name");
String employeeId = employeeRow.getJSONObject(i).getString("id");
//responseList.add(employeeName);
employee = new Employee(employeeName, employeeId);
employee.setEmployeeName(employeeName);
employee.setEmployeeId(employeeId);
employeeResponseList.add(employee);
Log.d(TAG, "onResponse: name = " + employeeName );
Log.d(TAG, "onResponse: id " + employeeId );
}
final ArrayAdapter<Employee> adapter =
new ArrayAdapter<Employee> (getApplicationContext(),android.R.layout.simple_dropdown_item_1line, employeeResponseList);
mAutoCompleteTextViewVisitee.setAdapter(adapter);
mAutoCompleteTextViewVisitee.setThreshold(1);
final Employee finalEmployee = employee;
mAutoCompleteTextViewVisitee.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "onItemClick: employee name = " + finalEmployee.getEmployeeName() );
Log.d(TAG, "onItemClick: employee id = " + finalEmployee.getEmployeeId() );
}
});
}