Ummm...  don't do that?  Your slow code is doing tons of work, so it is
going to be slow.  If you just can't avoid doing all of that work for each
item, you might need to fall back on the approach of formatting the text in
a separate thread, based on the list view API demo for when retrieving item
data is slow.

On Sun, Jan 11, 2009 at 10:10 AM, Mariano Kamp <[email protected]>wrote:

> Hi,
>   I have a performance problem in a ListView. The problem seems to be that
> it takes too long to setup each individual row. It scrolls very, very
> painfully slow, because of that.
>
>   The actual code renders an Entry differently depending on the read state
> and if it is changed. When not using this code and just plainly called
> setText() on the TextViews it is reasonable fast.  Also the content is HTML.
>
>   Am I doing something terribly wrong here?
>
>
> <?xml version="1.0" encoding="utf-8"?>
> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android";
> android:layout_width="fill_parent" android:layout_height="wrap_content">
>
> <TextView android:id="@+id/feed_title" android:layout_width="fill_parent"
> android:layout_height="wrap_content" android:textSize="11sp"
> android:paddingTop="3px" />
> <TextView android:id="@+id/entry_title" android:layout_width="fill_parent"
> android:layout_height="wrap_content" android:layout_below="@id/feed_title"
> android:textSize="15sp" android:paddingBottom="3px" />
> </RelativeLayout>
>
> class EntryListAdapter extends BaseAdapter {
>
> [..]
>
> public View getView(int position, View convertView, ViewGroup parent) {
> View rowView = convertView != null ? convertView : activity
> .findViewById(R.layout.entry_row);
>
> if (rowView == null)
> rowView = inflater.inflate(R.layout.entry_row, parent, false);
>
> Entry entry = entries.get(position);
> --> slow version
> EntryViewHelper.populateEntryView(rowView, entry);
> <--
>              -- OR --
>                 --> (somewhat) fast version
> TextView entryTitleView = (TextView) rowView.findViewById(R.id.entry_title
> );
> TextView feedTitleView = (TextView) rowView.findViewById(R.id.feed_title);
>
>
> feedTitleView.setText(entry.getFeedTitle());
> entryTitleView.setText(entry.getTitle());
> <--
> return rowView;
> }
> }
>
> class EntryViewHelper {
> [..]
>
> static void populateEntryView(View view, Entry entry) {
>
> TextView entryTitleView = (TextView) view
> .findViewById(R.id.entry_title);
> TextView feedTitleView = (TextView) view.findViewById(R.id.feed_title);
>
> feedTitleView.setText(U.renderTitleToSpannedString(
> entry.getFeedTitle(), entry.getFeedTitleType()));
> entryTitleView.setText(U.renderTitleToSpannedString(entry.getTitle(),
> entry.getTitleType()));
>
> int style = Typeface.NORMAL;
> if (!entry.isRead())
> style |= Typeface.BOLD;
> if (entry.isReadStatePending())
> style |= Typeface.ITALIC;
>
> entryTitleView.setText(U.renderTitleToSpannedString(entry.getTitle(), entry
> .getTitleType()));
> feedTitleView.setText(U.renderTitleToSpannedString(
> entry.getFeedTitle(), entry.getFeedTitleType()));
>
> entryTitleView.setTypeface(Typeface.DEFAULT, style);
> }
> }
>
> ..
> import android.text.Html;
> import android.text.Spanned;
> import android.text.SpannedString;
> ..
> class U {
> static Spanned renderTitleToSpannedString(String titleString,
> String titleType) {
> Spanned title = null;
> if ("html".equals(titleType) || "xhtml".equals(titleType))
> title = Html.fromHtml(titleString);
> else
> title = new SpannedString(titleString);
> return title;
> }
> }
>
>   Btw. I re-use the convert view etc. The performance degradation only
> happens when I style the output.
>
> Cheers,
> Mariano
>
> >
>


-- 
Dianne Hackborn
Android framework engineer
[email protected]

Note: please don't send private questions to me, as I don't have time to
provide private support.  All such questions should be posted on public
forums, where I and others can see and answer them.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to