我正在尝试实现一个简单的 android REST 客户端,但在理解如何在我的活动之间传递数据时遇到了一些问题。
我有这个 ListActivity(我正在使用 Spring REST 模板):
public class MainActivity extends ListActivity
{
protected static final String TAG = MainActivity.class.getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(this, "You have selected" + position + id ,
Toast.LENGTH_SHORT).show();
}
@Override
protected void onStart() {
super.onStart();
new DownloadClientesTask().execute();
}
private void refreshClientes(List<Cliente> clientes) {
if (clientes == null) {
return;
}
ClientesListAdapter adapter = new ClientesListAdapter(this, clientes);
setListAdapter(adapter);
}
private class DownloadClientesTask extends AsyncTask<Void, Void, List<Cliente>> {
@Override
protected List<Cliente> doInBackground(Void... params) {
final String url = "http://192.168.1.119/~henry/api_slim/index.php/customers";
try {
// Set the Accept header for "application/json"
HttpHeaders requestHeaders = new HttpHeaders();
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
requestHeaders.setAccept(acceptableMediaTypes);
// Populate the headers in an HttpEntity object to use for the request
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
// Perform the HTTP GET request
ResponseEntity<Cliente[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity,
Cliente[].class);
// convert the array to a list and return it
return Arrays.asList(responseEntity.getBody());
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e(TAG, e.getMessage(), e);
}
return null;
}
@Override
protected void onPostExecute(List<Cliente> result) {
refreshClientes(result);
}
}
}
这是我的 listAdapter :
public class ClientesListAdapter extends BaseAdapter{
private List<Cliente> clientes;
private final LayoutInflater layoutInflater;
public ClientesListAdapter(Context context, List<Cliente> clientes) {
this.clientes = clientes;
this.layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return this.clientes != null ? clientes.size() : 0;
}
@Override
public Cliente getItem(int position) {
return this.clientes.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = this.layoutInflater.inflate(R.layout.cliente_list_item, parent, false);
}
Cliente cliente = getItem(position);
if (cliente != null) {
TextView t = (TextView) convertView.findViewById(R.id.name);
t.setText(cliente.getFirstname());
}
return convertView;
}
}
这是我得到的数据的 POJO 类:
public class Cliente {
private Integer id_customer;
private String firstname;
public Integer getId_customer() {
return id_customer;
}
public void setId_customer(Integer id_customer) {
this.id_customer = id_customer;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
}
当我从 listView 中选择一个元素时,我想在另一个活动或片段上显示有关此元素的特定详细信息,但我不知道如何从列表中获取此元素的 customer_id,我是否必须在处理时保存它响应?我需要使用内容提供者或数据库来提供这种行为吗?我真的很困惑,在此先感谢您的帮助!