Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better understanding of the SimpleAdapter's ViewBinder

I have 4 TextViews, 2 ImageViews, 2 Buttons and 2 widgets that are part of a row definition in a ListView. The data comes from XML and a SimpleAdapter. To access these TextViews I implement the ViewBinder in a custom class and override the setViewValue. This works and the two TextViews I want to dynamically change are handled in the setViewValue. What is confusing to me is why my other two TextViews don't get passed through setViewValue. I say this based upon setting a breakpoint where the execution thread only enters twice. I was expecting to see it 4 or more times?

Here is the setViewValue where I have a breakpoint set.

@Override     
public boolean setViewValue(View view, Object data, String text)
{
    if(view.getId() == R.id.txtvw1)
    {             
//blah do some stuff
    }
    else if (view.getId() == R.id.txtvw2)
    {
//Blah do some stuff
    }

    return true;
} 

xml declaration of the TextViews(1 shows and 4 doesn't):

<TextView
        android:id="@+id/txtvw1"
        android:layout_centerHorizontal="true"
        android:layout_width="185dp"
        android:layout_height="25dp"
        android:textSize="20sp"
        android:layout_marginTop="60dp"
        android:gravity="center"
        android:inputType="none"
        android:text="@string/str_StaticA"
        android:textColor="#C0F700" />

    <TextView
        android:id="@+id/txtvw4"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="35dp"
        android:layout_width="95dp"
        android:layout_height="50dp"
        android:textSize="18dp"
        android:layout_marginTop="110dp"
        android:gravity="center"
        android:inputType="none"
        android:text="IMHO:"
        android:textColor="#FFBA19" />

So in summary why does the execution enter the override only twice AND it just so happens to be the exact two I want to update?

Well through additional reading and deductive reasoning a few more pieces of information have come to light. In my mind ever View in the ListView row XML would pass through the ViewBinder but that was WRONG! From what I can figure you attach the ViewBinder to the DataAdapter via setViewBinder. Well since the DataAdapter is told what View(s) to use to populate the data in it it makes sense that only the two were showing. I was only revealing two to it.

Here is a snippet where I specify the Views involved in the populating process and pass them on the constructor of the DataAdapter:

String[] from = new String[] {"txtvw_PrevLift", "txtvw_PrevReps", "ActuLiftPikr", "ActulRepsPikr" };
int[] to = {R.id.txtvw_PrevLift, R.id.txtvw_PrevReps, R.id.ActuLiftPikr, R.id.ActulRepsPikr };

LiftDataAdapter LiftDataAdapter = new LiftDataAdapter(this, LiftDataMaps, R.layout.liftdatalayout, from, to);

While this seems to make sense I don't know that my deductive reasoning is accurate. Will update as I find more information.

like image 955
GPGVM Avatar asked May 18 '12 15:05

GPGVM


1 Answers

If you set a ViewBinder on SimpleAdapter all the Views you declared(in the to array in your case) will be passed to the ViewBinder's setViewValue() method no matter what. If the setViewValue method doesn't return true meaning the data binding for that View has failed(for whatever reason) then you get the default action(for a TextView), setting the text from the data Hashmap. Right now you declared the setViewValue to do something for only two TextViews(checking by their ids) and also to return true no matter what. When you'll enter the setViewValue for the other Views in your row layout you'll again enter the setViewValue, don't match the ids(so no updates for those Views) and return true(which will tell the adapter that the binding was successful and this View doesn't require any more work). A look at the source code for the SimpleAdapter.bindView method will show you how the ViewBinder is used.

Also, I see that you use a custom adapter, are you sure you don't interfere with the normal SimpleAdapter logic?(also don't use the same name for the class and variable name)

like image 128
user Avatar answered Sep 23 '22 13:09

user