Hah... the code I was going to post has a switch statement to get different layouts... but I thought you were only using one layout, so I didn't want to confuse the issue.

Lately, I have been using the below global (to the current Activity) type variables to keep track of which view I am showing, but this activity only show one type of layout at a time.

private enum currentLayoutTypes {MY_GROUPS, FIND_GROUPS, GROUP_MEMBERS};
    private currentLayoutTypes currentLayout;

I found when I was switching the view depending on item type that just setting the tag on the convertView when it was created and checking it to determine if it was the correct type worked well.

Something like below in the getView, and using different functions for each type of view to attach text and listeners :

if ((convertView == null) || ( (String) convertView.getTag() != "what I want"))
                 {
LayoutInflater inflater = Groups.this.getLayoutInflater();
                     switch (currentLayout)
                     {
case MY_GROUPS : { convertView = inflater.inflate(R.layout.mygroupitem, null); convertView.setTag("MY_GROUPS"); break;} case FIND_GROUPS : convertView = inflater.inflate(R.layout.findgroupitem, null); convertView.setTag("FIND_GROUPS"); break;} case GROUP_MEMBERS : convertView = inflater.inflate(R.layout.groupmember, null); convertView.setTag("GROUP_MEMBERS"); break;}
                     }


                 }
                 switch (currentLayout)
                 {
case MY_GROUPS : convertView = FillMyGroupsRow(convertView, position); case FIND_GROUPS : convertView = FillFindGroupsRow(convertView, position); case GROUP_MEMBERS : convertView = FillGroupMemberRow(convertView, position);
                 }









On 31/05/2010 1:51 PM, Christophe wrote:
ok,

I finally have understand what is going on :

I have different layout on my list (R.layout.step_list_item,
R.layout.step_list_item2 ...) for each type of item in my list. When I
insert a new item, android try to re-use the layout which was
previously in the list at the same position. And it use the wrong one
because the type of the item has changed ...

So I remove the "if (convertView == null)" test and re-inflate my
layout each time and everything works fine now.

morale : Premature optimization is the root of all evil.

So now I've got a new question  : isn't there a better way to do
this ? It really seem awfully complicated just to have a list with
different kind of items.

On May 31, 10:36 pm, Christophe<[email protected]>
wrote:
hello Brad, thanks for your reply.

Isn't the call to super.getView(...) suppose to do this ?? I already
told the adapter which layout to use (see the constructor of my
adpater on my first post), so it should know which one to inflate.

Anyways I tried something similar to what you suggested :

if (convertView == null) {
         convertView =
LayoutInflater.from(getApplicationContext()).inflate(R.layout.step_list_item,
null);

}

but I've got a similar nullPointerException when I call
"findViewById" :

EditText ed = (EditText)
convertView.findViewById(R.id.list_item_sb); // return null
ed.setText("test"); // will crash

Again, it works fine when the list is shown for the first time but it
crash when I use "adapter.notifyDataSetChanged()" ...
So "R.id.list_item_sb" is found in my layout the first time but not
the second time ???

This is getting confusing :(

On May 31, 10:07 pm, Brad Gies<[email protected]>  wrote:

You need something like this in the getView to create the view if it
hasn't been created already.:
                   if (convertView == null)
                   {
                       LayoutInflater inflater =
Groups.this.getLayoutInflater();
                      convertView = inflater.inflate(R.layout.groupitem,
null);
                       }
On 31/05/2010 12:58 PM, Christophe wrote:
hello,
I have written a custom ArrayAdapter for a ListActivity :
private class StepItemAdapter extends ArrayAdapter<Step>    {
            public StepItemAdapter(Context context, List<Step>    objects) {
                    super(context, R.layout.step_list_item, R.id.list_item_sb,
objects);
            }
            @Override
            public View getView(final int pos, View convertView, ViewGroup
parent) {
                    View v = super.getView(pos, convertView, parent); // will 
crash
here
                    // etc ...
            }
}
On the "onCreate" method of the activity I tell the activity to use
this adapter with a list of object I get from somewhere else
("t.getSteps()") :
adapter = new StepItemAdapter(this, t.getSteps());
this.setListAdapter(adapter);
Everything works fine and the activity is displayed. But if I add a
new object to the list and tell the adapter to refresh the view I've
got an error :
t.getSteps().add(3, new Step());
adapter.notifyDataSetChanged();
The "getView" method of my adapter is called, which is ok since the
list view need to be updated, but I've got an exception on the call to
"super.getView(pos, convertView, parent)".
The stack trace :
java.lang.NullPointerException
at
android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:
355)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
at pkr.tm.activities.EditTemplateActivity
$StepItemAdapter.getView(EditTemplateActivity.java:145)
at
android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:
220)
at android.widget.AbsListView.obtainView(AbsListView.java:1294)
at android.widget.ListView.makeAndAddView(ListView.java:1727)
at android.widget.ListView.fillDown(ListView.java:652)
at android.widget.ListView.fillSpecific(ListView.java:1284)
at android.widget.ListView.layoutChildren(ListView.java:1558)
at android.widget.AbsListView.onLayout(AbsListView.java:1147)
...
Do you guy have any ideas of what is going on ??
--
Sincerely,
Brad Gies
-----------------------------------------------------------------------
Bistro Bot - Bistro 
Blurbhttp://www.bgies.comhttp://www.bistroblurb.comhttp://www.bistrobot.com
-----------------------------------------------------------------------
Never doubt that a small group of thoughtful, committed people can
change the world. Indeed. It is the only thing that ever has - Margaret Mead


--
Sincerely,

Brad Gies
-----------------------------------------------------------------------
Bistro Bot - Bistro Blurb
http://www.bgies.com
http://www.bistroblurb.com
http://www.bistrobot.com
-----------------------------------------------------------------------

Never doubt that a small group of thoughtful, committed people can
change the world. Indeed. It is the only thing that ever has - Margaret Mead

--
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