gpt4 book ai didi

android - 如何通过单击列表项调用 Activity

转载 作者:行者123 更新时间:2023-11-30 01:40:42 26 4
gpt4 key购买 nike

下面提到了我的代码,不知道它有什么问题。我在 Fragment 中有 ListView ,想在单击列表项时调用 Activity 。

public class fren_fragment extends Fragment {
FrenListAdapter cAdapter;
ListView cListView;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.friend_fragment, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);

cAdapter = new FrenListAdapter(getActivity());
cListView = (ListView) getActivity().findViewById(android.R.id.list);
cListView.setAdapter(cAdapter);
cListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
cListView.setItemChecked(position,true);
Context context = view.getContext();
Intent intent = new Intent(context, CompanyDetailActivity.class);
context.startActivity(intent);
}
});

}
}

最佳答案

试试这个:

cListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
cListView.setItemChecked(position, true);

Intent intent = new Intent(getActivity(), CompanyDetailActivity.class);
getActivity().startActivity(intent);
}
});

关于android - 如何通过单击列表项调用 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34544550/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com