[mono-android] java translation, mark murphy 05 fancy viewlist example
Jonathan Pryor
jpryor at novell.com
Thu Mar 17 12:11:02 EDT 2011
There were two bugs with your translation.
On Mar 16, 2011, at 1:41 AM, Michael Isbell wrote:
> FILE 1
...
> public class LunchList : Activity
> {
> IList<Restaurant> model=new List<Restaurant>();
Change 1: make this a JavaList<Restaurant>, not a List<Restaurant>. Currently, IList<T> marshaling is "[In]" (to use a P/Invoke equivalent). That is, the contents of the list are copied into a Java-side java.util.ArrayList, and that copy is passed to Java code.
This is a problem because later, in LunchList.onSave(), you call adapter.Add(restaurant), which alters the java-side list but NOT the managed-side list.
By using JavaList<T> instead, you have a single list, so when the list is changed by Java code the changes are visible within managed code.
> FILE 2
...
> public class RestaurantAdapter : ArrayAdapter<Restaurant>
...
> public View getView(int position, View convertView,
> ViewGroup parent) {
Change 2: this is wrong. The Java code doesn't make it obvious, but this is supposed to override a base class method. (Specifically, the sample code _should_ have used @Override to make this clearer, but @Override is optional, so Java missing @Override doesn't break anything. Not overriding a method in C#, meanwhile, _does_ break things.)
Thus, you want:
public override View GetView(int position, View convertView, ViewGroup parent) {
With those two changes, the sample works for me.
- Jon
More information about the Monodroid
mailing list