ListView的android系统中从2动态字符串数组 [英] ListView in android from 2 dynamic strings arrays

查看:199
本文介绍了ListView的android系统中从2动态字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字符串数组,这里S1 []包含名称和S2 []列表包含与各自名称相关联的URL,现在我需要填充的ListView,名称和点击任何一个名字,我想启动一个意图在浏览器来处理URL。

我该怎么办呢?

解决方案

 公共类MyActivity扩展ListActivity {

私人的ArrayList<字符串>网址=新的ArrayList<字符串>();
私人的ArrayList<字符串>名称=新的ArrayList<字符串>();


@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    urls.add(http://www.google.com);
    names.add(谷歌);

    ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(这一点,android.R.layout.simple_list_item_1,android.R.id.text1,姓名);
    setListAdapter(适配器);

}

保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){
    字符串URL = urls.get(位置);

    意图I =新的意图(Intent.ACTION_VIEW);
    i.setData(Uri.parse(URL));
    startActivity(ⅰ);
}
 

}

I have two string arrays, here s1[] contains a list of names and s2[] contains URL's associated with the respective name, now i need to populate ListView, with the names and on clicking any of the names, i want to start an intent for the browser to handle the URL.

How do i do that?

解决方案

public class MyActivity extends ListActivity{

private ArrayList<String> urls = new ArrayList<String>();
private ArrayList<String> names = new ArrayList<String>();


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    urls.add("http://www.google.com");
    names.add("google");

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, names);
    setListAdapter(adapter);

}

protected void onListItemClick (ListView l, View v, int position, long id){
    String url = urls.get(position);

    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
}

}

这篇关于ListView的android系统中从2动态字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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