Why doesn't my ItemClick event trigger?
_specialtyListView.ItemClick += (s, e) => _viewModel.Specialty = (string)_specialtyListView.Adapter.GetItem((int)e.Id);
NOTE:
The listview is populated.
Items receive focus.
However, I just cannot get the itemclick event to trigger.
I have the following initialization code for my ListView:
_specialtyListView = FindViewById<ListView>(Resource.Id.SpecialtyListView);
_specialtyListView.ChoiceMode = ChoiceMode.Single;
_viewModel.LoadSpecialties();
_specialtyListView.Adapter = new SpecialtiesAdapter(this, new List<string>(_viewModel.Specialties));
_specialtyListView.ItemClick += (s, e) => _viewModel.Specialty = (string)_specialtyListView.Adapter.GetItem((int)e.Id);
I then have the following ListViewItem layout:
<ListView
android:id="#+id/SpecialtyListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
android:divider="#CCCCCC"
android:dividerHeight="1dp"
android:paddingLeft="2dp" />
Here's the Layout for my ListItem:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/ProviderSpecialty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp" />
</LinearLayout>
I suggest you switch to RecyclerView as it is has better memory handling; you can make updates to single items instead of being forced to update them all.
Anyways, I can't reproduce your issue locally. I did the following:
var listView = FindViewById<ListView>(Resource.Id.listview);
listView.ChoiceMode = ChoiceMode.Single;
listView.ItemClick += (s, e) =>
{
System.Diagnostics.Debug.WriteLine($"Clicked {e.Position}");
};
var items = new string[] { "Havarti", "Brie", "Cheddar", "Muenster", "Swiss" };
var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, items);
listView.Adapter = adapter;
I used the exact same layout as you posted. The ItemClick handler is hit consistently.
There's a bug where the breakpoint will NOT be triggered when the code is NOT wrapped in between braces:
listview.ItemClick += (s, e) => selectedItem = listview.GetItemAtPosition(e.Position).ToString();
To observe the breakpoint, I had to format the code as the following:
listview.ItemClick += (s, e) =>
{
selectedItem = listview.GetItemAtPosition(e.Position).ToString();
};
Related
I just started using using oxyplot library for my project. I successfully added a donut chart in my project but the problem with this is that it takes up the whole screen to show the graph. Is tried so many way to solve this but none of them worked.
Here is my axml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
local:layout_behavior="#string/appbar_scrolling_view_behavior" >
<OxyPlot.Xamarin.Android.PlotView
android:id="#+id/plot_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:MvxBind="Model DonutChart"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
local:MvxBind="Text TestText1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
local:MvxBind="Text TestText2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
local:MvxBind="Text TestText3" />
</LinearLayout>
Here is the graph method:
private void GeneratePlotModel()
{
var plotmodel = new PlotModel();
var seriesP1 = new PieSeries
{
Stroke = OxyColors.Transparent,
StrokeThickness = 2.0,
InsideLabelPosition = 0.0,
AngleSpan = 360,
StartAngle = 0,
InnerDiameter = 0.7,
OutsideLabelFormat = null,
InsideLabelFormat = null,
TickHorizontalLength = 0.0,
TickRadialLength = 0.0
};
seriesP1.Slices.Add(new PieSlice("Test 1", test1) { IsExploded = false, Fill = OxyColors.Blue });
seriesP1.Slices.Add(new PieSlice("Test 2", test2) { IsExploded = false, Fill = OxyColors.Green });
seriesP1.Slices.Add(new PieSlice("Test 3", test3) { IsExploded = false, Fill = OxyColors.Red });
plotmodel.Series.Add(seriesP1);
DonutChart = plotmodel;
}
Here is my plotmodel property:
private PlotModel _donutChart ;
public PlotModel DonutChart
{
get => _donutChart ;
set
{
if (_donutChart == value)
return;
_donutChart = value;
RaisePropertyChanged(nameof(DonutChart ));
}
}
It takes up the whole screen to show the graph because you set
android:layout_width="match_parent"
android:layout_height="match_parent"
And you can make it only takes patial space by adding
android:layout_weight="1"
So the layout is like this:
<OxyPlot.Xamarin.Android.PlotView
android:id="#+id/plot_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
local:MvxBind="Model DonutChart"/>
I Want To List All sqlite database table,
When i tries to List the table contents It Only Shows the First columns of table.
AXML Page:;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:clickable="true"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:id="#+id/listView1" />
cs Page:
string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "BookmarkSqlite.db3");
var db = new SQLiteConnection(dpPath);
var data = db.Table<BookmarkSqliteTable>();
foreach (var listing in data)
{ var from = new string[]{ listing.SiteName + "----" + listing.SiteUrl };
//var from = fromm.ToList();
ListView listtable = (ListView)FindViewById(Resource.Id.listView1);
listtable.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, from);
}
}
I use a Xamarin.Forms layout, and it has a Stepper control.
On Android, it looks like this:
Now it seems that I have to create custom cells on each platform as the UWP performance is way-way-way below the normal scrolling speed.
According to this page, the Stepper is rendered via a custom view in Android:
I created an Android layout, and I intend to use the StepperRenderer, but I don't know how to inflate the custom view inside my custom viewcell:
<LinearLayout
android:id="#+id/Layout1"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dip">
<TextView
android:id="#+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF7F3300"
android:textSize="20dip"
android:textStyle="italic" />
<!-- NumberPicker is not applicable-->
<NumberPicker
android:id="#+id/Stepper"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!--StepperRenderer
android:id="#+id/Stepper"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /-->
<Spinner
android:id="#+id/Picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
protected override Android.Views.View GetCellCore(Xamarin.Forms.Cell item, Android.Views.View convertView, ViewGroup parent, Context context)
{
var x = (MyViewCell)item;
var view = convertView;
if (view == null)
{
// no view to re-use, create new
view = (context as Activity).LayoutInflater.Inflate(Resource.Layout.MyViewCellLayout, null);
}
view.FindViewById<TextView>(Resource.Id.Text).Text = x.Text;
view.FindViewById<NumberPicker>(Resource.Id.Stepper).Value = x.Value;
return view;
}
Any idea how I could use that view in my viewCell?
There is similar thread related to this:
https://forums.xamarin.com/discussion/26173/custom-renderer-for-stepper
Hope this helps.
I am creating a simple application to get familiar with Xamarin. I want to create and populate a spinner and display its options dynamically. I have seen the documentation here but it is not created programmatically. Any help will be appreciated
var levels = new List<String>() { "Easy", "Medium", "Hard", "Multiplayer" };
var adapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleSpinnerItem, levels);
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
var spinner = FindViewById<Spinner>(Resource.Id.spnrGameLevel);
spinner.Adapter = adapter;
spinner.ItemSelected += (sender, e) =>
{
var s = sender as Spinner;
Toast.MakeText(this, "My favorite is " + s.GetItemAtPosition(e.Position), ToastLength.Short).Show();
};
My axml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Choose your game level" />
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/spnrGameLevel" />
</LinearLayout>
To dynamically create items for Spinner, you need to have an adapter.
The simplest adapter would be ArrayAdapter<T>.
Here is the example.
var items = new List<string>() {"one", "two", "three"};
var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerItem, items);
After setting up the adapter, find the Spinner in your view and set adapter to it.
var spinner = FindViewById<Spinner>(Resource.Id.spinner);
spinner.Adapter = adapter;
Here is the code of my custom adapter... it doesn't call my get view... returns only white blank page without nothing. I don't know what is the problem. Does anybody can help. Thanks a lot previously!
public class ProductShopsAdapter extends ArrayAdapter<ProductShop> {
private Context cntx;
private ArrayList<ProductShop> shopValues;
ProductShopsHolder holder = null;
public ProductShopsAdapter(Context context, int textViewResourceId, ArrayList<ProductShop> stringValues) {
super(context,textViewResourceId);
shopValues = stringValues;
cntx = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ProductShopsHolder();
LayoutInflater layoutInflater = (LayoutInflater)cntx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.shops_list, null);
//Holder Elements initialization
holder.setShopName((TextView)convertView.findViewById(R.id.shop_name));
holder.setPrice((TextView)convertView.findViewById(R.id.shop_part_price));
convertView.setTag(holder);
} else {
holder = (ProductShopsHolder) convertView.getTag();
}
holder.getShopName().setText(getItem(position).getShopName());
holder.getPrice().setText(getItem(position).getPrice());
return convertView;
}
}
and here is my XML
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProductShopsActivity" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/footer"
android:padding="0dp" >
</ListView>
<include
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#android:id/list"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
layout="#layout/footer_menu" />
</RelativeLayout>
and single view :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#f2f2f2"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:id="#+id/shop_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Technomarket"
android:textSize="14sp" />
<TextView
android:id="#+id/shop_part_price"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="30 лв."
android:textSize="18sp" />
<TextView
android:id="#+id/details_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableRight="#drawable/rightarrow"
android:gravity="center"
android:text="Детайли"
android:textColor="#289bd6"
android:textSize="14sp" />
You don't need ArrayList inside your ArrayAdapter - ArrayAdapter can store items itself. And that's why getView() doesn't get called - default implementation of getCount() returns the number of items in ArrayAdapter itself. If you keep your items in your own ArrayList, at least override getCount() to return shopValues.getCount(). Otherwise it is always 0 and the ListView to which your adapter is bound doesn't even know there are any items to dsplay at all.