ListView控件打开Android中的超链接 [英] ListView to open hyperlinks in android

查看:459
本文介绍了ListView控件打开Android中的超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法中显示类似名称  TechCrunch的 蠢 美国航空航天局 在列表视图,当用户点击其中一个,就应该打开的链接,相应的上市网站的意图。

Is there any way in to Show Names like "TechCrunch" "Twit" "NASA" in the listview and when the user clicks on one of them , it should open an intent with the link for corresponding Websites listed.

任何想法AP preciated。

Any ideas appreciated.

推荐答案

如果您不希望的ListView 是由数据库生成的,你要添加的每个网站上的的ListView的适配器自己,那么你可以做到以下几点。

If you don't want the ListView to be generated by a database and you want to add every website to the adapter of your ListView yourself, then you can do the following.

您需要创建一个项目类,如下所示:

You would have to create an Item class, that looks like this:

public class Item {

    private String mName;
    private int mLink;

    public Item(String name, int link) {

        this.mName = name;
        this.mLink = link;
    }

    public int getLink() {

        return mLink;
    }

    public void setLink(int link) {

        this.mLink = link;
    }

    public String getName() {

        return mName;
    }

    public void setName(String name) {

        this.mName = name;
    }

    @Override
    public String toString() {

        return this.mName;
    }
}

,然后设置你的ListView的适配器是这样的:

and then set the adapter of your ListView like this:

List<Item> dataForTheAdapter = new ArrayList<Item>();

// add some data
dataForTheAdapter.add(new Item("Apple", "www.Apple.com"));
dataForTheAdapter.add(new Item("Microsoft", "www.Microsoft.com"));
dataForTheAdapter.add(new Item("Google", "www.Google.com"));

mContext = MyActivity.this;
// R.layout.row is a layout, that contains only one TextView
mAdapter = new ArrayAdapter<Item>(mContext, R.layout.row, dataForTheAdapter);

ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(mAdapter);

现在设置 OnItemClickListener 的ListView

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        String url = mAdapter.getItem(position).getLink();
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    }
});

这篇关于ListView控件打开Android中的超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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