我有一个用于显示员工图片和姓名的自定义列表,我想在单击列表项时导航到下一个活动,在下一个活动中,我想从 Web 服务动态显示员工的详细信息!如何做到这一点,请给出一些想法或链接?
先感谢您!
1 回答
That's a very broad question which will take a long time to answer. There are a few ways you could do it, but hopefully this short answer will help and at least point you in a direction.
I'm guessing you already have an adapter with a list of all employees (List or whatever) powering your listview. You should create a public method in your adapter to take in the position, and return a unique id referencing your employee. Note a unique id is not necessarily the same as the position of the item.
Here's a crude example:
public int getUniqueId(int position){
Employee employee = mAllItems.get(position);
if(employee != null){
return employee.getUniqueId();
}
//Couldn't find
return 0;
}
Then implement an onItemClickListener on your listview. The method can then call the adapter to retrieve a unique id. This unique ID is what you would pass via intent to your new activity. Your new activity will lookup the employee details and display them.
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int uniqueId = mAdapter.getUniqueId(position);
if(uniqueId > 0) {
Intent i = new Intent(this, ActivityEmployeeDetails.class);
i.putExtra(ActivityEmployeeDetails.TAG_UNIQUE_ID, uniqueId);
startActivity(i);
}
}
Hope this helps.