[mono-android] v.FindViewById<Button>(Resource.Id.btnRemove) return null
Asmaa Mohamed Roushdy
asmaa at victorylink.com
Sun Mar 20 04:35:00 EDT 2011
I created custom datagrid activity as follow
[Activity(Label = "GridView", MainLauncher = true)]
public class MyGridView : Activity
{
Context mContext;
List<Quote> books = new List<Quote>();
LitemItemAdapter mcqListAdapter;
private List<string> symbols = new List<string>();
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
mContext = this;
books.Add(new Quote("RAYA.CA", 20, 30));
books.Add(new Quote("TMGH.CA", 30, -10));
books.Add(new Quote("ORTE.CA", 22, 20));
symbols.Add("RAYA.CA");
symbols.Add("TMGH.CA");
symbols.Add("ORTE.CA");
symbols.Add("AAPL.CA");
symbols.Add("CSI.CA");
symbols.Add("TELE.CA");
SetContentView(Resource.Layout.GridViewDesign);
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this,
Android.Resource.Layout.SimpleDropDownItem1Line, symbols);
AutoCompleteTextView textView =
(AutoCompleteTextView)FindViewById(Resource.Id.stockChosser);
textView.Adapter = adapter;
ListView bookListView =
(ListView)FindViewById(Resource.Id.bookListView);
mcqListAdapter = new LitemItemAdapter(this, books);
bookListView.Adapter = mcqListAdapter;
textView.ItemClick += new
EventHandler<ItemEventArgs>(textView_ItemClick);
}
void textView_ItemClick(object sender, ItemEventArgs e)
{
books.Add(new Quote(((TextView)e.View).Text, 10, 10));
mcqListAdapter.NotifyDataSetChanged();
}
}
class LitemItemAdapter : BaseAdapter,
Android.Views.View.IOnClickListener
{
Context mContext;
List<Quote> books;
public LitemItemAdapter(Context context, List<Quote> objects)
{
mContext = context;
}
public override int Count { get { return books.Count; } }
public override long GetItemId(int position)
{
return position;
}
public override Java.Lang.Object GetItem(int position)
{
return books[position];
}
public override View GetView(int position, View convertView,
ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater vi =
(LayoutInflater)mContext.GetSystemService(Context.LayoutInflaterService);
v = vi.Inflate(Resource.Layout.RawLayout, null);
}
Quote item = books[position];
if (item != null)
{
TextView symbolView =
(TextView)v.FindViewById(Resource.Id.symbol);
TextView lastPriceView =
(TextView)v.FindViewById(Resource.Id.lastPrice);
TextView changePercView =
(TextView)v.FindViewById(Resource.Id.changePerc);
Button removeButton =
v.FindViewById<Button>(Resource.Id.btnRemove);
if (symbolView != null)
{
symbolView.Text = item.getSymbol();
}
if (lastPriceView != null)
{
lastPriceView.Text = item.getLastPrice().ToString();
}
if (changePercView != null)
{
changePercView.Text = item.getChangePerc().ToString();
if (item.getChangePerc() < 0)
{
changePercView.SetTextColor(Android.Graphics.Color.Red);
}
else
{
changePercView.SetTextColor(Android.Graphics.Color.Green);
}
}
if (removeButton != null)
{
removeButton.SetOnClickListener(this);
removeButton.Id = position;
}
}
return v;
}
public void OnClick(View v)
{
books.RemoveAt(v.Id);
NotifyDataSetChanged();
}
}
public class Quote : Java.Lang.Object
{
private string symbol;
private decimal lastPrice;
private decimal changePerc;
public Quote(string symbol, decimal lastPrice, decimal changePerc)
: base()
{
this.symbol = symbol; this.lastPrice = lastPrice;
this.changePerc = changePerc;
}
public string getSymbol()
{
return symbol;
}
public void setSymbol(string symbol)
{
this.symbol = symbol;
}
public decimal getLastPrice()
{
return lastPrice;
}
public void setLastPrice(decimal lastPrice)
{
this.lastPrice = lastPrice;
}
public decimal getChangePerc()
{
return changePerc;
}
public void setChangePerc(decimal changePerc)
{
this.changePerc = changePerc;
}
}
RawLayout.xaml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget0"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="#ffffff"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button android:layout_width="wrap_content" android:id="@+id/btnRemove"
android:text="Remove" android:layout_height="40dip"></Button>
<TextView android:id="@+id/symbol" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text=" " android:textSize="13sp"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text=" "
android:id="@+id/changePerc" android:textSize="13sp"
android:layout_weight="1"></TextView>
<TextView android:id="@+id/lastPrice" android:layout_width="wrap_content"
android:text=" " android:layout_height="wrap_content"></TextView>
</LinearLayout>
But at position = 0 btnRemove is null, why?
Sincerely
Asmaa Mohamed Roushdy
Team Lead
<http://www.victoryfn.com/> Victory Finance
5 El Zohour st. 1st floor, Cairo 12311 Egypt
Phone: +2 02 37624480, Fax: +2 02 37624490
Mobile: +2 0144442156
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/mailman/private/monodroid/attachments/20110320/d31f415b/attachment-0001.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/png
Size: 181 bytes
Desc: not available
Url : http://lists.ximian.com/mailman/private/monodroid/attachments/20110320/d31f415b/attachment-0001.png
More information about the Monodroid
mailing list