我得到了使用loopj从使用此代码获取其余服务的所有员工的列表。
public class MainActivity extends ActionBarActivity {
private ListView employeeList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getEmployees();
}
private void getEmployees() {
List<Header> headers = new ArrayList<Header>();
headers.add(new BasicHeader("Accept", "application/json"));
EmployeeRestClient.get(MainActivity.this, "RestExample/employee",
headers.toArray(new Header[headers.size()]),null,
new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
ArrayList<Employee> employeeArray = new ArrayList<Employee>();
EmployeeAdapter employeeAdapter = new EmployeeAdapter(MainActivity.this, employeeArray);
for (int i = 0; i < response.length(); i++) {
try {
employeeAdapter.add(new Employee(response.getJSONObject(i)));
} catch (JSONException e) {
e.printStackTrace();
}
}
employeeList = (ListView) findViewById(R.id.list_employees);
employeeList.setAdapter(employeeAdapter);
}
});
}
我希望获得员工列表,select * from employees where age = ?
因此,在getEmployees(Long age)
方法中,如何通过RestExample/employee/{age}
答案 0 :(得分:1)
在this
内初始化ListView
(employeeList
),数据源(employeeArray
)和适配器(employeeAdapter
),而不是onCreate
中的onSuccess
方法。
employeeList = (ListView) findViewById(R.id.list_employees);
employeeArray = new ArrayList<Employee>();
employeeAdapter = new EmployeeAdapter(MainActivity.this, employeeArray);
employeeList.setAdapter(employeeAdapter);
您的getEmployees
方法,
private void getEmployees(Long age) {
List<Header> headers = new ArrayList<Header>();
headers.add(new BasicHeader("Accept", "application/json"));
EmployeeRestClient.get(MainActivity.this, "RestExample/employee/" + age,
headers.toArray(new Header[headers.size()]), null,
new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
employeeArray.add(new Employee(response.getJSONObject(i)));
} catch (JSONException e) {
e.printStackTrace();
}
}
// notify the adapter that the data has changed
employeeAdapter.notifyDataSetChanged();
}
});
}