0

Ok so I'm trying to add 100+ buttons to a listview via CustomAdapter and each button should open a different url. Say my starting url is www.example.com/hi/01.htm and my last url is www.example.com/hi/136.htm . My CustomAdapter moves down the list with the int position. So im thinking it should be something like this:

 URL url = new URL("www.example.com/hi/", position); 

But it doesn't work, plus the htm tag at the end wouldn't appear. Ive searched on google and found lots of things like URi and retrofit but I can't seem to understand how they might work with my specific needs. Any help/tips/advice would be highly apreciated!

2 Answers 2

2

Try android.net.Uri instead of URl like this

Uri uri = Uri.parse("www.example.com/hi/" + pos + ".htm");

Greets

Sign up to request clarification or add additional context in comments.

Comments

1

Use String.format() to add the variable to the string. In the following example, the %s will get replaced with the position variable.

URL url = new URL(String.format("http://www.example.com/hi/%s.htm", position));

Note

You were getting MalformedURLException because your example url does not specify the protocol.

From the documentation:

Throws: MalformedURLException - if no protocol is specified, or an unknown protocol is found, or spec is null.

2 Comments

I get malformed URL exception
@DavidGarciaBallester try now.

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.