gpt4 book ai didi

android - 如何从 fragment 中的listview的onitemclick事件调用startactivity或调用intent?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:29:01 27 4
gpt4 key购买 nike

我有一个 fragment ,它有自己的布局。在布局中,有一个 ListView ,我附加了 onitemclick 监听器,它将在单击列表行时启动/打开一个 Intent 。不幸的是,我总是收到这个错误:

从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志

但我不喜欢设置标志 Activity 。我无法打开 SamplePage.class

public class FrontPageFragment extends Fragment
{

private ArrayList<Order> m_orders = null;
private OrderAdapter m_adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View fragmentView=inflater.inflate(R.layout.frontpage, container, false);

m_orders = new ArrayList<Order>();
this.m_adapter = new OrderAdapter(this.getActivity().getApplicationContext(), R.layout.name, m_orders);

View eView=(View)fragmentView.findViewById(R.id.approvedOrders);
ListView listView=(ListView)eView.findViewById(R.id.listview);

listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

Intent intent = new Intent(view.getContext(), SamplePage.class);
intent.putExtra("id", "1");
view.getContext().startActivity(intent);

}
});

//bind data
listView.setAdapter(this.m_adapter);


return fragmentView;

}

最佳答案

当我复制你的代码时,我得到了同样的错误。所以我稍微改变了一下,它起作用了。检查这个:

Intent intent = new Intent(getActivity().getBaseContext(), Sampleclass.class);

intent.putExtra("id", 1);

startActivity(intent);

关于android - 如何从 fragment 中的listview的onitemclick事件调用startactivity或调用intent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7177633/

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