ListView控件打开Android中的超链接控件、超链接、ListView、Android

2023-09-05 03:14:39 作者:沒了感覺了,還有什麽可說

有什么办法中显示类似名称  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);
    }
});