简体   繁体   English

如何使用Fragment关联webView和listView项目

[英]how can I relate webView and listView items using Fragment

In my app, there is a fragment class and webView class with simple xml files. 在我的应用程序中,有一个带有简单xml文件的fragment类和webView类。 I want to click a ListView item(mail, stack etc.), then loading their URL's. 我想单击一个ListView项(邮件,堆栈等),然后加载其URL。 I could not it. 我做不到 please help. 请帮忙。 This is fragment class: 这是片段类:

public class frags extends Fragment {
   final String[] items={"google", "facebook", "twitter", "mail", "stack"};

//there are some codes. //有一些代码。

    myListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id)  {

           intent = new Intent ( context, web.class);
           startActivity(intent);
        }
    });
    return vw;    }

} }

In your Listview Click Listener Pass your Clicked Iitem Like this 在您的Listview中,Click Listener传递您的被单击项,像这样

 myListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id)  {

           intent = new Intent ( context, web.class);
           intent.putExtra("item_name",items.get(position));
           startActivity(intent);
        }
    });

and in your web.class receive item name 并在您的web.class中接收项目名称

Bundle b = getIntent.getExtras;
if(b.getString("item_name").equals("facebook")){
//open facebook url
}else if (b.getString("item_name").equals("twitter")){
//open twitter url
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM