本文介绍了Android的 - ListView控件的onClick()打开一个网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图打开一个网站时,我点击从list.For例子项目时点击项目'A'开放点击b打开另一个网站。
怎么做?谢谢。

 公共类MainActivity延伸活动{    私人的ListView LV;    ///////////
     ///////////////////////
    //列表视图适配器
    ArrayAdapter<串GT;适配器;    //搜索的EditText
    的EditText inputSearch;
    // ArrayList的列表视图为
    ArrayList的<&HashMap的LT;字符串,字符串>> productList的;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        //列表视图数据
        字符串产品[] = {戴尔Inspiron,HTC One X的,HTC野火S,HTC Sense的,HTC感觉XE
                iPhone 4S的,三星Galaxy Note 800
                三星Galaxy S3,MacBook Air的,Mac Mini的,MacBook Pro的};        LV =(ListView控件)findViewById(R.id.list_view);
        inputSearch =(EditText上)findViewById(R.id.inputSearch);        //添加项目列表视图
        适配器=新ArrayAdapter<串GT;(这一点,R.layout.list_item,R.id.product_name,产品);
        lv.setAdapter(适配器);        inputSearch.addTextChangedListener(新TextWatcher(){            @覆盖
            公共无效onTextChanged(CharSequence的CS,INT ARG1,ARG2 INT,INT ARG3){
                //当用户更改文本
                。MainActivity.this.adapter.getFilter()过滤器(CS);
            }            @覆盖
            公共无效beforeTextChanged(CharSequence的为arg0,ARG1 INT,INT ARG2,
                                          INT ARG3){
                // TODO自动生成方法存根            }            @覆盖
            公共无效afterTextChanged(编辑为arg0){
                // TODO自动生成方法存根
            }
        });    }
    AdapterView.OnItemClickListener adapterClick =新AdapterView.OnItemClickListener(){
        @覆盖
        公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
            //要执行的事情
            ListView控件lvMoogle5 =(ListView控件)findViewById(R.id.list_item);
            lvMoogle5.setOnItemClickListener(adapterClick);
            。的String [] =链接getResources()getStringArray(R.array.link);            URI URI = Uri.parse(链接[位置]);
            意向意图=新意图(Intent.ACTION_VIEW,URI);
            startActivity(意向);
        }
    };
}

的strings.xml

 <资源>
    <字符串名称=APP_NAME>&的Listview LT; /串>    <字符串名称=参考hello world>世界,你好<!/串>
    <字符串名称=action_settings>设置< /串>
    <字符串数组名=链接>
        <项目> HTTP://finalfantasy.wikia.com/wiki/Sephiroth_(Crisis_Core_Boss)LT; /项目>
        &LT;项目&GT; HTTP://finalfantasy.wikia.com/wiki/Kefka_Palazzo< /项目&GT;
        &LT;项目&GT; HTTP://finalfantasy.wikia.com/wiki/Exdeath_(老板)LT; /项目&GT;
        &LT;项目&GT; HTTP://finalfantasy.wikia.com/wiki/Kefka_Palazzo< /项目&GT;
        <item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item><item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
        <item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item><item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
        <item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item><item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
        &LT;项目&GT; HTTP://finalfantasy.wikia.com/wiki/Exdeath_(老板)LT; /项目&GT;
    &LT; /字符串数组&GT;
&LT; /资源&GT;

我试试这个方法从这里,但不起作用。


解决方案

 公共类MainActivity延伸活动{
    的String [] =网站{
              雅虎,
              谷歌,
              脸谱
      };
@覆盖
   保护无效的onCreate(捆绑savedInstanceState){
      super.onCreate(savedInstanceState);
      的setContentView(R.layout.activity_mainlist);      ArrayAdapter&LT;串GT;适配器=新ArrayAdapter&LT;串GT;(这一点,
      R.layout.activity_listview,网页);      ListView控件列表=(ListView控件)findViewById(R.id.list);
      list.setAdapter(适配器);      list.setOnItemClickListener(新AdapterView.OnItemClickListener(){
          @覆盖
            公共无效onItemClick(适配器视图&LT;&GT;母公司,观景,
                                    INT位置,长的id){
                开关(位置){
                情况下0:字符串URL =yahoo.com;                    意图I =新意图(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(URL));
                    startActivity(ⅰ);
                打破;
             }            }
        @燮pressWarnings(未使用)
        公共无效的onClick(视图v){
            };
        });   } //结束的OnCreate
} // endactivity

I am trying to open a website when I clicked a item from the list.For example when click item 'A' open www.google.com click 'b' open another website.How to do that? thanks.

public class MainActivity extends Activity {

    private ListView lv;

    ///////////


     ///////////////////////
    // Listview Adapter
    ArrayAdapter<String> adapter;

    // Search EditText
    EditText inputSearch;


    // ArrayList for Listview
    ArrayList<HashMap<String, String>> productList;

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

        // Listview Data
        String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
                "iPhone 4S", "Samsung Galaxy Note 800",
                "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};

        lv = (ListView) findViewById(R.id.list_view);
        inputSearch = (EditText) findViewById(R.id.inputSearch);

        // Adding items to listview
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
        lv.setAdapter(adapter);



        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                MainActivity.this.adapter.getFilter().filter(cs);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
            }
        });

    }
    AdapterView.OnItemClickListener adapterClick = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //要執行的事情
            ListView lvMoogle5 = (ListView)findViewById(R.id.list_item);
            lvMoogle5.setOnItemClickListener(adapterClick);
            String[] links = getResources().getStringArray(R.array.link);

            Uri uri = Uri.parse(links[position]);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    };
}

strings.xml

<resources>
    <string name="app_name">Listview</string>

    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string-array name="link">
        <item>http://finalfantasy.wikia.com/wiki/Sephiroth_(Crisis_Core_Boss)</item>
        <item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
        <item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item>
        <item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
        <item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item><item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
        <item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item><item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
        <item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item><item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
        <item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item>
    </string-array>


</resources>

I try this method from here but don't work. http://ithelp.ithome.com.tw/question/10106920

解决方案
public class MainActivity extends Activity {


    String[] web = {
              "Yahoo",
              "Google",
              "Facebook"
      };
@Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_mainlist);

      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
      R.layout.activity_listview, web);

      ListView list = (ListView) findViewById(R.id.list);
      list.setAdapter(adapter);

      list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                switch(position){
                case 0:  String url = "yahoo.com";

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


             }

            }
        @SuppressWarnings("unused")
        public void onClick(View v){
            };
        });

   }//end oncreate


}//endactivity

这篇关于Android的 - ListView控件的onClick()打开一个网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-13 23:08