1

I am trying to create a ListView that contains Webview's. The webview is being loaded by a diffrent url every time

@Override
public View getView(int position, View convertView, ViewGroup parent)
{

    View rowView = convertView;
    if (rowView == null)
    {

        // res = getLayoutInflater().inflate(R.layout.item_composer, null);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.card, parent, false);

        ViewHolder holder = new ViewHolder();

        holder.webview = (WebView) rowView.findViewById(R.id.card_webview);
        holder.webview.getSettings().setJavaScriptEnabled(true);

        holder.webview.setWebViewClient(new WebViewClient());

        rowView.setTag(holder);

    }

    ViewHolder holder = (ViewHolder) rowView.getTag();

    holder.webview.loadUrl(cardList.get(position).getUrl());//loading webview with URL




    return rowView;
}

I managed to implement it successfully but it is acting very weird(the webview doesnt always load the correct url) . I came across this question Which one to use WebView or TextView in a list with HTML data in it? and @CommonsWare suggests that this is not possible.Is there any way to do this?

Community
  • 1
  • 1
user1163234
  • 2,407
  • 6
  • 35
  • 63
  • 1
    Can you describe "very weird"? I think the "very weird" topic is the question right? – GhostDerfel Jan 15 '14 at 13:23
  • I added to the description. When scrolling the listview it doesnt always load the correct url they seemd to be mixed up... – user1163234 Jan 15 '14 at 13:29
  • Sorry for asking that, but have you tested your code without the webview? Try to just set a diferent background color on each item using the same logic you are using, I will test your code in 30 min. if you can wait :) – GhostDerfel Jan 15 '14 at 13:36
  • Sorry my bad I loaded the arraylist in a for so it was multiplying by 5 – user1163234 Jan 15 '14 at 14:11
  • Why did CommonsWare suggest not using WebView in ListView? – user1163234 Jan 15 '14 at 14:26
  • 2 points here. 1 - The webview element use more memory then a TextView with formatted HTML; 2 - The webview also implement scroll and things could get massy when you have a ScrollView with a ScrollView inside, like your user will try to Scroll the list down but he will start to Scroll the webview instead – GhostDerfel Jan 15 '14 at 14:29
  • Thanks for your time!How can I implement the TextView with Formatted HTMl when all I have is a url? – user1163234 Jan 15 '14 at 14:43

1 Answers1

0

The user already found the solution on our discusion in the comments:

Sorry my bad I loaded the arraylist in a for so it was multiplying by 5

This answer come to the last comment, where the user ask how he can display a HTML page on a TextView.

First you need to load the HTML on a String, you can do that using an AsyncTask and something like that:

HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(url); HttpResponse response = client.execute(request);

String html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
    str.append(line);
}
in.close();
html = str.toString();

Then you need to set the HTML on your TextView, remember to do that on a UI thread.

textView.setText(Html.fromHtml(html));
GhostDerfel
  • 1,533
  • 1
  • 11
  • 18