[mono-android] java translation, mark murphy 05 fancy viewlist example

Jonathan Pryor jpryor at novell.com
Wed Mar 16 11:11:20 EDT 2011


Your C# source is incomplete, as your FILE 1 source and FILE 2 source are the same file; you're missing your translation of LunchList.

Furthermore, there's a difference in RestaurantAdapter.getView(): Java uses a 3 parameter inflate() call, while you use a 2 parameter Inflate() call, so this is a semantic difference (I assume, anyway).

Thus, the primary question is this: what's the crash? Specifically, what's the `adb logcat` output around this error? It may provide useful information. :-)

I've also noticed that the managed stack traces aren't always helpful, so to get better context I'll often do:

	try {
		// code which throws...
	}
	catch (Java.Lang.Throwable t) {
		Log.Error ("*jonp*", t, "something failed!");
	}

This will print out the java-side stack trace + error message on `adb logcat`, and is often useful as well.

If that fails, please file a bug with a full test case attached.

Thanks,
 - Jon

On Mar 16, 2011, at 1:41 AM, Michael Isbell wrote:

> ok, this is a little lengthy.
> 
> it's a direct translation of mark murphy's 05 fancy view list tutorial from his book
> 
> this compiles perfectly-- but crashes on the run when "	adapter.add(r);" gets called as far as I can tell
> 
> anyone see anything obvious?
> 
> a) I know there are issues with generic
> b) yes i've seen the versions of code that use base adapter, trying to use Murphy's concepts closely
> c) i have the whole project in my dropbox if anyone would like to try compiling and running with it
> 
> I would think it would be helpful for a novell person to look at this, as this seems pretty straightforward.
> 
> hey, maybe it's just a stupid programmer error :-) that would be nice...
> 
> Thanks
> Mike
> 
> 
> 
> 
> 
> here's the code
> 
> --------------translated THIS (Listview with custom array adapter)-------------
> 
> package apt.tutorial;
> 
> import android.app.Activity;
> import android.os.Bundle;
> import android.view.View;
> import android.view.ViewGroup;
> import android.view.LayoutInflater;
> import android.widget.ArrayAdapter;
> import android.widget.Button;
> import android.widget.EditText;
> import android.widget.ImageView;
> import android.widget.ListView;
> import android.widget.RadioGroup;
> import android.widget.TextView;
> import java.util.ArrayList;
> import java.util.List;
> 
> public class LunchList extends Activity {
> 	List<Restaurant> model=new ArrayList<Restaurant>();
> 	RestaurantAdapter adapter=null;
> 	
> 	@Override
> 	public void onCreate(Bundle savedInstanceState) {
> 		super.onCreate(savedInstanceState);
> 		setContentView(R.layout.main);
> 		
> 		Button save=(Button)findViewById(R.id.save);
> 		
> 		save.setOnClickListener(onSave);
> 		
> 		ListView list=(ListView)findViewById(R.id.restaurants);
> 		
> 		adapter=new RestaurantAdapter();
> 		list.setAdapter(adapter);
> 	}
> 	
> 	private View.OnClickListener onSave=new View.OnClickListener() {
> 		public void onClick(View v) {
> 			Restaurant r=new Restaurant();
> 			EditText name=(EditText)findViewById(R.id.name);
> 			EditText address=(EditText)findViewById(R.id.addr);
> 			
> 			r.setName(name.getText().toString());
> 			r.setAddress(address.getText().toString());
> 			
> 			RadioGroup types=(RadioGroup)findViewById(R.id.types);
> 			
> 			switch (types.getCheckedRadioButtonId()) {
> 				case R.id.sit_down:
> 					r.setType("sit_down");
> 					break;
> 					
> 				case R.id.take_out:
> 					r.setType("take_out");
> 					break;
> 					
> 				case R.id.delivery:
> 					r.setType("delivery");
> 					break;
> 			}
> 			
> 			adapter.add(r);
> 		}
> 	};
> 	
> 	class RestaurantAdapter extends ArrayAdapter<Restaurant> {
> 		RestaurantAdapter() {
> 			super(LunchList.this, R.layout.row, model);
> 		}
> 		
> 		public View getView(int position, View convertView,
> 												ViewGroup parent) {
> 			View row=convertView;
> 			RestaurantHolder holder=null;
> 			
> 			if (row==null) {													
> 				LayoutInflater inflater=getLayoutInflater();
> 				
> 				row=inflater.inflate(R.layout.row, parent, false);
> 				holder=new RestaurantHolder(row);
> 				row.setTag(holder);
> 			}
> 			else {
> 				holder=(RestaurantHolder)row.getTag();
> 			}
> 			
> 			holder.populateFrom(model.get(position));
> 			
> 			return(row);
> 		}
> 	}
> 	
> 	static class RestaurantHolder {
> 		private TextView name=null;
> 		private TextView address=null;
> 		private ImageView icon=null;
> 		private View row=null;
> 		
> 		RestaurantHolder(View row) {
> 			this.row=row;
> 			
> 			name=(TextView)row.findViewById(R.id.title);
> 			address=(TextView)row.findViewById(R.id.address);
> 			icon=(ImageView)row.findViewById(R.id.icon);
> 		}
> 		
> 		void populateFrom(Restaurant r) {
> 			name.setText(r.getName());
> 			address.setText(r.getAddress());
> 	
> 			if (r.getType().equals("sit_down")) {
> 				icon.setImageResource(R.drawable.ball_red);
> 			}
> 			else if (r.getType().equals("take_out")) {
> 				icon.setImageResource(R.drawable.ball_yellow);
> 			}
> 			else {
> 				icon.setImageResource(R.drawable.ball_green);
> 			}
> 		}
> 	}
> }
> --------------------------------------------------------------   INTO THIS C# MONODROID VERSION ---------------------------------------------------------------------
> 
> FILE 1
> 
> using System;
> 
> using Android.App;
> using Android.Content;
> using Android.Runtime;
> using Android.Views;
> using Android.Widget;
> using Android.OS;
> using System.Collections;
> using System.Collections.Generic;
> 
> namespace murphytutorial1
> {
> 
> public class RestaurantAdapter : ArrayAdapter<Restaurant>
> 	{
> 	
> 		Activity context;
> 		IList<Restaurant> model;
> 		int resource;
> 		
> 		public RestaurantAdapter(Activity context, int resource, IList<Restaurant> model) //We need a context to inflate our row view from
> 			: base(context, resource, model)
> 		{
> 			
> 			this.context = context;
> 			this.model = model;
> 			this.resource = resource;
> 				
> 		}
> 			
> 		public View getView(int position, View convertView,
> 												ViewGroup parent) {
> 		
> 			View row=convertView;
> 			RestaurantHolder holder=null;
> 			
> 			if (row == null) {
> 				LayoutInflater layoutInflater = context.LayoutInflater;
> 				row = layoutInflater.Inflate(this.resource,parent);
> 				holder = new RestaurantHolder(row);
> 				row.Tag =  holder;
> 			}
> 			else {
> 				holder = (RestaurantHolder) row.Tag;
> 			}
> 			
> 			holder.populateFrom(model[position]);
> 		
> 			return row;
> 		}
> 		
> 	}
> 
> }
> 
> FILE 2
> 
> using System;
> 
> using Android.App;
> using Android.Content;
> using Android.Runtime;
> using Android.Views;
> using Android.Widget;
> using Android.OS;
> using System.Collections;
> using System.Collections.Generic;
> 
> namespace murphytutorial1
> {
> 
> public class RestaurantAdapter : ArrayAdapter<Restaurant>
> 	{
> 	
> 		Activity context;
> 		IList<Restaurant> model;
> 		int resource;
> 		
> 		public RestaurantAdapter(Activity context, int resource, IList<Restaurant> model) //We need a context to inflate our row view from
> 			: base(context, resource, model)
> 		{
> 			
> 			this.context = context;
> 			this.model = model;
> 			this.resource = resource;
> 				
> 		}
> 			
> 		public View getView(int position, View convertView,
> 												ViewGroup parent) {
> 		
> 			View row=convertView;
> 			RestaurantHolder holder=null;
> 			
> 			if (row == null) {
> 				LayoutInflater layoutInflater = context.LayoutInflater;
> 				row = layoutInflater.Inflate(this.resource,parent);
> 				holder = new RestaurantHolder(row);
> 				row.Tag =  holder;
> 			}
> 			else {
> 				holder = (RestaurantHolder) row.Tag;
> 			}
> 			
> 			holder.populateFrom(model[position]);
> 		
> 			return row;
> 		}
> 		
> 	}
> 
> }
> 
> /*
> 
> FILE 3
> 
> using System;
> 
> using Android.App;
> using Android.Content;
> using Android.Runtime;
> using Android.Views;
> using Android.Widget;
> using Android.OS;
> using System.Collections;
> using System.Collections.Generic;
> 
> namespace murphytutorial1
> {
>  public class RestaurantHolder : Java.Lang.Object
> 	{
> 		private TextView name=null;
> 		private TextView address=null;
> 		private ImageView icon=null;
> 		private View row=null;
> 		
> 		public RestaurantHolder (View row)
> 		{
> 			this.row=row;
> 			
> 			name=(TextView)row.FindViewById(Resource.Id.title);
> 			address=(TextView)row.FindViewById(Resource.Id.address);
> 			icon=(ImageView)row.FindViewById(Resource.Id.icon);
> 		}
> 		
> 		public void populateFrom(Restaurant r) 
> 		{
> 			name.Text = r.Name;
> 			address.Text = r.Address;
> 			
> 			if (r.Type.Equals("sit_down")) {
> 				icon.SetImageResource(Resource.Drawable.Icon); // drawable.ball_red);
> 			}
> 			else if (r.Type.Equals("take_out")) {
> 				icon.SetImageResource(Resource.Drawable.Icon); // SetImageResource(r.drawable.ball_yellow);
> 			}
> 			else {
> 				icon.SetImageResource(Resource.Drawable.Icon); // SetImageResource(r.drawable.ball_green);//
> 			}
> 			
> 		}
> 		
> 	}
> }
> 
> */
> 
> 
> 
> -- 
> Mike Isbell
> 
> 
> _______________________________________________
> Monodroid mailing list
> Monodroid at lists.ximian.com
> 
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid



More information about the Monodroid mailing list