Android - ListView onClick() open a website

戏子无情 提交于 2019-12-06 16:13:39

问题


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


回答1:


Try out this code:-

lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Object obj = lv.getAdapter().getItem(position);
            Uri uri = Uri.parse(obj.toString());
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });



回答2:


You did code for item click but you missed to assign for ListView.

Add below code after lv = (ListView) findViewById(R.id.list_view); line.

 lv = (ListView) findViewById(R.id.list_view);
 lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                     String[] links = getResources().getStringArray(R.array.link);
                     Uri uri = Uri.parse(links[position]);
                     Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                     startActivity(intent);

                }
            });

Note : Your Product array size and Link Array size must be same otherwise your app will be crash due to ArrayIndexOutOfBound Expection




回答3:


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



回答4:


Try this code

              lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {


                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
                                Bundle bn = new Bundle();


                                LinkModel model = (LinkModel) parent.getItemAtPosition(position);


                                String a = model.getLink ();

                                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(a));
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity ( intent );
                                bn.putInt("pos", position);
                                intent.putExtras(bn);

                            }
                        });


来源:https://stackoverflow.com/questions/29743535/android-listview-onclick-open-a-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!