1

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

2
  • What do you mean by don't work? What's the error in the logcat? Commented Apr 20, 2015 at 9:11
  • Do you know how to give a item specific url to link ? Commented Apr 22, 2015 at 8:46

4 Answers 4

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);
        }
    });
Sign up to request clarification or add additional context in comments.

3 Comments

Your answer is same as the answer given by @CapDroid.
@PrerakSola from which angle you find my answer same other than the onclick implementation and the Intent part. I only provided an alternate approach to do get the values.
I don't think that there's any issue with this answer. Correct answers are bound to look similar.
0

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

1 Comment

But when I used the search bar it will index to the wrong url,can I assign every string name to a specific url? thanks
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

Comments

0

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

                            }
                        });

1 Comment

Answers which are only providing code are not considered complete. Please check stackoverflow.com/help/how-to-answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.