简体   繁体   中英

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. I want to click a ListView item(mail, stack etc.), then loading their URL's. 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

 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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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