Android - ListView onClick()打开一个网站

时间:2015-04-20 09:07:03

标签: android listview android-button

当我点击列表中的某个项目时,我正在尝试打开一个网站。例如,点击项目“A”打开www.google.com点击“b”打开另一个网站。 怎么做?感谢。

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>

我从这里尝试这种方法但不起作用。 http://ithelp.ithome.com.tw/question/10106920

4 个答案:

答案 0 :(得分:1)

试用这段代码: -

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);
        }
    });

答案 1 :(得分:0)

您为项目点击设置了代码,但您错过了为ListView分配的内容。

lv =(ListView)findViewById(R.id.list_view); 行后添加以下代码。

 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);

                }
            });

注意:您的产品数组大小和链接数组大小必须相同,否则您的应用会因ArrayIndexOutOfBound Expection

而崩溃

答案 2 :(得分:0)

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

答案 3 :(得分:0)

尝试此代码

              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);

                            }
                        });