如何调用startactivity或致电意图从列表视图中onitemclick事件的片段? [英] How to call startactivity or call intent from onitemclick event of listview in fragment?

查看:153
本文介绍了如何调用startactivity或致电意图从列表视图中onitemclick事件的片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了自己的布局片段。在布局,有一个ListView和我连接onitemclick监听器将启动/打开意图列表行被点击时。不幸的是,我总是得到这个错误:

从活动背景以外调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志

但我并不preFER设置标志的活动。我无法打开SamplePage.class

 公共类FrontPageFragment扩展片段
{

    私人的ArrayList<排序> m_orders = NULL;
    私人OrderAdapter m_adapter;

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                             捆绑savedInstanceState){

        查看fragmentView = inflater.inflate(R.layout.frontpage,集装箱,假);

        m_orders =新的ArrayList<排序>();
        this.m_adapter =新OrderAdapter(this.getActivity()getApplicationContext(),R.layout.name,m_orders);

        查看eView的=(查看)fragmentView.findViewById(R.id.approvedOrders);
        ListView控件的ListView =(ListView控件)eView.findViewById(R.id.listview);

         listView.setOnItemClickListener(新OnItemClickListener(){
            公共无效onItemClick(适配器视图<>母公司视图中查看,
                INT位置,长的id){

                意向意图=新的意图(view.getContext(),SamplePage.class);
                intent.putExtra(ID,1);
                。view.getContext()startActivity(意向);

            }
         });

        //绑定数据
        listView.setAdapter(this.m_adapter);


        返回fragmentView;

    }
 

解决方案

当我复制乌尔code,我得到了同样的错误。所以我改变一点点,和它的工作。检查:

 意向意图=新的意图(getActivity()getBaseContext(),Sampleclass.class。);

intent.putExtra(身份证,1);

startActivity(意向);
 

I have a fragment that has its own layout. In the layout, there is a listview and i attached onitemclick listener that will start/open an intent when the list row is clicked. Unfortunately, I always get this error :

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag

But I do not prefer to set the flag activity. I could not open the 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;

    }

解决方案

When I copied ur code I got the same error. So I changed it a little bit, and it worked. Check this:

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

intent.putExtra("id", 1);

startActivity(intent);

这篇关于如何调用startactivity或致电意图从列表视图中onitemclick事件的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆