What's different between Arraylist and Arrayadapter - arraylist

look at this code and tell if i use
Arraylist whats happen ..i want to know whats a diffrent between ArrayList and Arrayadaptor?
public class structNote {
public String title;
public String description;
public boolean done;
}
public class AdapteNote extends ArrayAdapter<structNote> {
public AdapteNote(ArrayList<structNote> array) {
super(G.context, R.layout.adaptornote, array);
}

ArrayList is list which holds list of int, String...
ArrayAdapter is adapter, which is used for Listing items in screen, like for ListView. It has more functions for that, if item removed in background, ArrayAdapter will remove it as well if you call adapter.notifyDatasetChanged(), it updates screen. You can not use ArrayList as adapter for Listing items on screen, but you can use it for holding list of items in memory.
Picture below ArrayAdapter holds items inside ListView. You can not use ArrayList for such listing.

Related

Enums and selection lists in XAML

I've been at this all day and could now use some help. Thank you in advance.
Enums cannot be declared as partial; classes can. I had the need to combine multiple enum lists (which have assigned integers that are unique across enum groups) in a single list that is selectable in XAML using an attached property. I have looked a numerous things and I am still lost.
First, let's say I have the following code:
public enum Items {One, Two, Three};
public class DepObj : DependencyObject
{
public static readonly DependencyProperty
Field = DependencyProperty.RegisterAttached("Field", typeof(Items), typeof(DepObj), new PropertyMetadata(Items.One, OnFieldChanged));
public static Items GetField(DependencyObject d)
{
return (Items)d.GetValue(Field);
}
public static void SetField(DependencyObject d, Items field)
{
d.SetValue(Field, field);
}
private static void OnFieldChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
SetField(d, (Items)e.NewValue);
}
}
Now, in XAML, I can set an Items property to "One" for instance. In XAML, I get a droplist of items that may be selected (One, Two, Three). Is there something special built into the XAML editor for enums that knows how to display this list? Or, is there something I can specify on any class that XAML would call within the class to provide this selection list?
I have looked at System.Enum (Microsoft System.Enum) and what it inherits from and I don't see what I need to do to provide equivalent functionality for my own class (or even a struct, if value types are required).
So my question is twofold:
How does XAML provide selection lists for enums
Is this functionality available to embed/use in classes that XAML will recognize
Thank you in advance.

Application to display food names

I am trying to learn, and I want to create a application that works like this: When they click a button, it will choose a random food(for example baked potato), and whey they click the label(or button) with the food, the recipe for that food will open in a browser.
I have tried making a list or some sort, but not sure how to do this:
<local:People x:Food="ArrayFood">
<sys:String Pasta="One" URL="http://food.com/pasta"/>
<sys:String Corn="Two" URL="http://food.com/corn"/>
<sys:String Salsa="Three" URL="http://food.com/Salsa"/>
</local:People>
I think the problem you're running into is the String type doesn't have those properties of Pasta, Corn, Salsa, or URL.
Probably the easiest thing is not to store that in the resource dictionary at all. I would store a list of custom objects in the ViewModel.
Something like:
public class Food
{
public Food() {}
public Food(string name, string url) {Name=name;Url = url;}
public string Name {get; set;}
public string Url {get; set;}
}
public class MyViewModel
{
public List<Food> Foods {get;set;} = new List<Food>
{
new Food("Corn", "http://..."),
new Food("Pasta", "http://blah/pasta" )
}
.. other view model stuff ..
}
Then bind it to a list view. When they tap, then visit the url

Populating a single Listview with multiple custom array of objects in Xamarin

I am new to xamarin/Mobile development. I got a task to create a single listview which should populate the objects of two different classes using xamarin.forms.
`Class A
{
string PendingRequestID;
string PendingRequestStatus;
string PendingRequest;
}
Class B
{
String CompletedRequestId;
String ApprovedByUsername;
DateTime CompletedTime
}
`
above are the two different entities and I need to populate the list of objects of both in single list view. Each object is having its own separate UI layout.
How can i specify multiple ItemSource in a ListView in xamarin.forms?
Please help me.
Just create a property of type ObservableCollection<object> for ItemsSource on ListView and use a data template selector in order to provide item-type based template(s) to ListView.
An example can be found here
EDIT - 1 : Sample code
class MyDataTemplateSelector : Xamarin.Forms.DataTemplateSelector
{
public MyDataTemplateSelector()
{
// Retain instances!
this._typeADataTemplate = new DataTemplate(typeof(TypeAViewCell));
this._typeBDataTemplate = new DataTemplate(typeof(TypeBViewCell));
}
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
if (item == null)
return null;
return (item is A) ? this._typeADataTemplate : this._typeBDataTemplate;
}
private readonly DataTemplate _typeADataTemplate;
private readonly DataTemplate _typeBDataTemplate;
}
You can't assign multiple types for ItemSource. Therefore, one way is to use a third class as a base class and then derive your A and B from it. Then use this C type as ItemSource.
Class C{
...
}
Class A : C
{
string PendingRequestID;
string PendingRequestStatus;
string PendingRequest;
}
Class B : C
{
String CompletedRequestId;
String ApprovedByUsername;
DateTime CompletedTime
}
Note that you will then need to check for the right subtype during runtime whenever you click on an Item in the list.
Hope it helps!

Convert ArrayList to Observable List for JavaFX program?

I am a semi-beginner Java programmer, learning Java FX from a variety of sources. In my program, I would like to create a ComboBox, and populate the choices with the toString() output of a series of objects from an ArrayList. Here, pizza toppings are defined as an object. They are created and stored in a PizzaMgr object, basically a glorified wrapper for an ArrayList:
public class Topping{
private String name;
public Topping(String a){
this.name=a;
}
public String toString(){
return this.name;
}
}
//=================================================
import java.util.ArrayList;
public class PizzaMgr{
private ArrayList<Topping> OrderedToppings;
public PizzaMgr(){
OrderedToppings = new ArrayList<Topping>();
OrderedToppings.add(new Topping("Pepperoni"));
OrderedToppings.add(new Topping("Mushrooms"));
OrderedToppings.add(new Topping("Onions"));
}
public ArrayList<Topping> getList(){
return OrderedToppings;
}
}
So far, so good. But the hitch I hit is when I want a ComboBox to list all of those items in the PizzaMgr's ArrayList. Ideally, I'd like to use this ComboBox constructor:
ComboBox<T>(ObservableList<T> items)
The problem? How to extract all the ArrayList items into an Observable List? I've been reading up on Arraylists, Observable Lists, interfaces in general, but I can't figure out how to get this to work. I've read that an ArrayList is a Collection, and Observable List can be an interface to a Collection, so I thought I was home free. However, when I try to implement the ComboBox constructor:
import javafx.scene.control.*;
public class Menu{
public static void main(String[] args){
PizzaMgr m = new PizzaMgr();
ComboBox<Topping> topMenu = new ComboBox<Topping>(m.getList());
}
}
I get the compiler error:
Menu.java:18: error: incompatible types: ArrayList<Topping> cannot be converted to ObservableList<Topping>
ComboBox<Topping> topMenu = new ComboBox<Topping>(m.getList());
^
So obviously my ArrayList isn't seen as an Observable List.
What stumps me is: How can I present my ArrayList to the ComboBox constructor, making it seem like an Observable List? Is it a syntax slight-of-hand? Or do I have to convert the ArrayList into another data structure in advance?
Many thanks,
-RAO
ObservableList is a sub interface (specialized version of) List. (ObservableList adds functionality for observing changes to the list.) ArrayList is a particular implementation of List, but is not an implementation of ObservableList. Hence you can pass an ArrayList anywhere a List is expected, but you cannot pass an ArrayList if an ObservableList is expected.
As something of an aside, note it's not really recommended to expose the implementation type, but you should really just expose the interface type:
import java.util.List;
import java.util.ArrayList;
public class PizzaMgr{
private List<Topping> orderedToppings;
public PizzaMgr(){
orderedToppings = new ArrayList<Topping>();
orderedToppings.add(new Topping("Pepperoni"));
orderedToppings.add(new Topping("Mushrooms"));
orderedToppings.add(new Topping("Onions"));
}
public List<Topping> getList(){
return orderedToppings;
}
}
To create an ObservableList from a regular List, you can use
ComboBox<Topping> topMenu
= new ComboBox<Topping>(FXCollections.observableList(m.getList()));
which creates a new ObservableList that "wraps" the array list you provide. I.e. calling get(index) or add(item) on the observable list simply delegates to the list you provide.
You could also do
ComboBox<Topping> topMenu
= new ComboBox<Topping>(FXCollections.observableArrayList(m.getList()));
which would create a new observable list and copy all the elements from the list you provide into it. So subsequently manipulating one list would not change the other.
To create an observableList you need to, first of all, have a predefined list eg an arrayList. Pass the list to an instance of observableList as below...
//Assuming you had this for a list
List<String> list = new ArrayList<String>();
//Pass it over in this manner
ObservableList<String> observableList = FXCollections.observableList(list);
//Then add a listener
observableList.addListener (new ListChangeListener() {
//override the method here
}
Hope it will help...
ArrayList<Site> sites = new ArrayList < Site > ();
sites.add(new Site("Google", "http://www.google.com"));
ObservableList<Site> siteList = FXCollections.observableArrayList(sites);
final ComboBox comboBox = new ComboBox(siteList);
Button btn = new Button();
btn.setText("Read comboBox");
btn.setOnAction(new EventHandler < ActionEvent > () {
#Override
public void handle(ActionEvent event) {
label.setText("selected: " + comboBox.getValue());
Site site = (Site) comboBox.getSelectionModel().getSelectedItem();
System.out.println(site.name);
}
});

Share a common observable collection for both list and grid layout in long list selector?

I have a list of items that are displayed in portrait orientation in a wp8 app. I have defined GroupHeaderTemplate and JumpListStyle, so it neatly shows as a grouped list. I created a KeyedList class that represents a list grouped by a key and my ViewModel exposes ObservableCollection of KeyedList.
using System.Collections.Generic;
using System.Linq;
namespace Timbre.Collections
{
public class KeyedList<TKey, TItem> : List<TItem>
{
public TKey Key
{
get;
protected set;
}
public KeyedList(TKey key)
: base()
{
Key = key;
}
public KeyedList(TKey key, IEnumerable<TItem> items)
: base(items)
{
Key = key;
}
public KeyedList(IGrouping<TKey, TItem> grouping) :
base(grouping)
{
Key = grouping.Key;
}
}
}
The Property in the ViewModel is as follows:
public ObservableCollection<KeyedList<string, Album>> Albums
{
get { return this.groupedAlbums; }
private set
{
if (this.groupedAlbums == value) return;
this.groupedAlbums = value;
RaisePropertyChanged(() => this.Albums);
}
}
The LongListSelector binds to this ObservableCollection and works properly.
Now when the orientation of the page changes to landscape, I want to change the layout mode from List to Grid on the LongListSelection and I want to get rid of the grouping as it takes quite some space and I just want to display a grid of pictures.
Typically I would set this on the long list when orientation changes to landscape:
IsGroupingEnabled=false
But I'm no longer able to bind to the same ObservableCollection I used before, as it exposes a list of lists and not a simple list. Now I know this can be solved by exposing 2 ObservableCollections in the ViewModel, one for grouped list and one for simple list. But typically, I want to show the same item that was visible to the user in the flat list when the layout mode changes to grid. Is there some way to just expose one ObservableCollection and yet just change the LayoutMode from List to Grid and make it work?