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

Michael Isbell michael.isbell at gmail.com
Wed Mar 16 01:41:25 EDT 2011


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/mailman/private/monodroid/attachments/20110316/07f457c2/attachment-0001.html 


More information about the Monodroid mailing list