当用户单击 listFragment 的项目时,我想启动配置文件活动。该列表显示有关用户的不同信息,例如:姓名、ID、排名...
我正在寻找的是一种访问有关单击的项目的信息并将列表中选择的用户的 id 传递给意图的方法。
AsyncTask 从服务器检索信息并调用适配器以在列表项中显示用户的详细信息。如果我在 onListItemClick 中使用:
Object o=(Object)getListView().getItemAtPosition(position);
Log.e("LikeAttend Report", "Clicked list item: " + position + " Content: " + o.toString());
日志返回:
点击列表项:1 内容:{score=7540, id=4, name= Gepp}
如何在方法中获取 id 值以将 Extra 添加到 Intent 并启动配置文件活动?
我在参考http://developer.android.com/reference/java/lang/Object.html中没有找到任何有用的东西
代码:
这是我迄今为止尝试过的:
public static class LikeFragmentList extends ListFragment
{
private ListView listLike;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View likeView = inflater.inflate(R.layout.likelist, container, false);
ProgressBar progress = (ProgressBar) likeView.findViewById(R.id.loading_spinner_like);
String tabSocial="like";
listLike = (ListView) likeView.findViewById(android.R.id.list);
/*
* The asyncTask retrieves infos about the user list
*/
new AsyncLoadSocial(getActivity(), progress, listLike).execute(uid,tabSocial);
return likeView;
}
@Override
public void onListItemClick(ListView listView, View view, int position, long id)
{
super.onListItemClick (listView, view, position, id);
// I've tried with object and cursor
Object o = (Object) getListView().getItem(position);
//Cursor cursor = (Cursor) getListAdapter().getItem(position); NullPointerException
Log.e("LikeAttend Report", "Clicked list item: " + position + " Content: " +o.toString());
Intent intent = new Intent(getActivity(), Profile.class);
//intent.putExtra("id", o.toString(0)); NullPointerException
getActivity().startActivity(intent);
}
}
Emmanuel提出的第一个解决方案。
在 ListFragmentList 中:
@Override
public void onListItemClick(ListView listView, View view, int position, long id)
{
super.onListItemClick (listView, view, position, id);
String infos = getListView().getItemAtPosition(position).toString();
String[] info = infos.split("=");
Log.e("LikeAttend Report", "Clicked list item: " + position + " Content: total -> " + infos + " \n Info[2] is:" + info[2]);
Intent intent = new Intent(getActivity(), Profile.class);
//intent.putExtra("id", o.toString(0));
getActivity().startActivity(intent);
}
日志返回:
点击列表项:2 内容:total -> {score=0, id=12, photo=, name=Beatrice Fratocchi} Info[2] is: 12, photo
其中所需的信息是 12。
* 提出的第二种解决方案 *
@SuppressWarnings("unchecked")
@Override
public void onListItemClick(ListView listView, View view, int position, long id)
{
super.onListItemClick (listView, view, position, id);
HashMap<String, String> map = (HashMap<String, String>) getListView().getItemAtPosition(position);
Log.e("LikeAttend Report", "Clicked list item: " + position +" Content: \n" + map.get(TAG_ID).toString());
Intent intent = new Intent(getActivity(), Profile.class);
intent.putExtra("id", map.get(TAG_ID));
getActivity().startActivity(intent);
}