Search code examples
javaandroidjsoup

Adding a link in list view


So I am currently working on a project and I have very little idea of Android studio. So I have a code in which I am parsing details of products from e-commerce websites and I am creating a list on android studio and printing that in the listview. I created a ArrayAdpator for that.

public void adaptormethod(){
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main2Activity.this, android.R.layout.simple_list_item_1,allproducts);

        listview.setAdapter(adapter);
    }

The array Adaptor looks like this and I am parsing the details using jsoup. So I have the link ready for products but how to activate it? There are like 20 products on the list so how can I add a link for every item on that list? If you can give a detailed answer, it would be better. Thank you.


Solution

  • You can open a webpage when an item is clicked using setOnItemClickListener methods on your ListView. For example like this:

    yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
                // open a web page 
                String url = adapter.getItem(position);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);
            }
        });