MergeAdapter with custom layouts (TextViews and ImageViews)

979 views
Skip to first unread message

David C. Sousa

unread,
Apr 16, 2011, 4:43:06 PM4/16/11
to cw-android
Hello,

I've been reading and testing the provided source code, that we get
at:

https://github.com/commonsguy/cwac-merge/

Right now, I've been using the MergeAdapter.java and the SackOfViews
from the JAR. However, I need to use custom XMLs that have both
TextViews and ImageViews, and somehow they are not working. I believe
this has something to do with the use of ArrayAdapters. Anyway... I'd
like to use this for section header (header.xml):

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_header_title"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:padding="10dip"
android:gravity="center_vertical"
android:text="Header example"
android:textSize="14sp"
android:textStyle="bold"
/>

And this for each row (row.xml): (real layout is more complicated, but
this is a minimal example of what I need)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
<ImageView
android:id="@+id/row_img_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:src="@drawable/ic_up_off"
/>

<TextView
android:id="@+id/row_hour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text="00:00 am"

android:textSize="22sp"
/>
</ LinearLayout>

However, I'm clueless as how to use MergeAdapterDemo to address the
two layouts. I created a stackoverflow question here, just in case
someone wants to know what I'm trying to do (it shows how I managed to
get it to work using another method):

http://stackoverflow.com/questions/5684002/android-license-problem-using-sectioned-listview-with-j-sharkeys-separatedlis

I thank in advance everyone that is willing to help me out,
David

Mark Murphy

unread,
Apr 16, 2011, 4:52:53 PM4/16/11
to cw-an...@googlegroups.com
You would inflate the header XML yourself using LayoutInflater and add
that to the MergeAdapter using addView(). You would use the row
adapter with an ArrayAdapter (or whatever sort of adapter you want),
adding that to the MergeAdapter using addAdapter().

--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Android Training in NYC: http://marakana.com/training/android/

David C. Sousa

unread,
Apr 16, 2011, 10:56:46 PM4/16/11
to cw-android
Thank you for guiding the way. After trial-and-error, I got it.

Anyway, since I'm used to helping people on the Internet, I know there
is nothing worse than having someone say "I got it right", and not
posting the solution. Or even worse: not posting an explanation of the
solution. So here it is for those searching Google. For the header:

private View header (String caption) {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);

TextView tv = (TextView) View.inflate(
this, R.layout.tide_header, null);
tv.setText(caption);
return tv;
}

Calling from onCreate:

adapter.addView(header("Section 1"), false);
...
adapter.addView(header("Section 2"), false);

For the rows... each row/item is an
ArrayList<HashMap<String,Object>>(). After "put"ting each HashMap into
the ArrayList, we get a "row". Then, do the same process for each row
in the section. Example:

rowList = new ArrayList<HashMap<String,Object>>();
HashMap<String, Object> hashMap;

// START REPEATING HERE, for each item in a row
hashMap= new HashMap<String, Object>();
hashMap.put(ITEM_NAME, "David");
hashMap.put(ITEM_HEIGHT, "1.82");
hashMap.put(ITEM_LEVEL, R.drawable.ic_level03);
rowList .add(hm);
// END REPEATING HERE

PeopleAdapter ta = new PeopleAdapter(rowList, this);

adapter=new MergeAdapter();
adapter.addView(header("Family 1"), false);
adapter.addAdapter(ta);
adapter.addView(header("Family 2"), false);
...

The Adapter is the easiest part. You can use pretty much any adapter
you currently use (if you ever did a custom XML, single level List,
you'll know).

Best regards everyone. And thanks Mark Murphy! Your help may not have
sounded much to you, but it certainly was to me.
David



On Apr 16, 5:52 pm, Mark Murphy <mmur...@commonsware.com> wrote:
> You would inflate the header XML yourself using LayoutInflater and add
> that to the MergeAdapter using addView(). You would use the row
> adapter with an ArrayAdapter (or whatever sort of adapter you want),
> adding that to the MergeAdapter using addAdapter().
>
> --
> Mark Murphy (a Commons Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy
Reply all
Reply to author
Forward
0 new messages