How do I create an interface for the following?
void push ( int value ) //adds value to list appropriately for a stack
int pop ( ) //remove and return value from list
boolean isEmpty() //returns true if the stack is empty
int size() //returns number of items in stack
:
Related
class CustomStack<T> {
final _list = <T>[];
void push(T value) => _list.add(value);
T pop() => _list.removeLast();
T get top => _list.last;
bool get isEmpty => _list.isEmpty;
bool get isNotEmpty => _list.isNotEmpty;
int get length => _list.length;
#override
String toString() => _list.toString();
}
void main() {
CustomStack<String> plates = CustomStack();
//Add plates into the stack
plates.push("Plate1");
plates.push("Plate2");
plates.push("Plate3");
plates.push("Plate Extra");
print(plates);
print(plates[plates.length-1]);
}
I get an error in the last line "The operator '[]' isn't defined for the type 'CustomStack'."
How can I control the index in the stack.
I want to print only "Plate Extra" on the screen.
There is no need to use that structure plates[plates.length-1] if getting the last element is possible with the built function. If you want to get the last item in Custom Stack, you can define a function in your Custom Stack.
T get peek => _list.last;
i am trying to use static list as global variable that can be accessible through all the dart classes , so i have done like that:
class MyData {
// initialize it to an empty list
static var List<MyObject> myObjectList = new List<MyObject>();
}
then in another class I load that empty list with list of MyObject (no doubt that it loaded with success)
I do add objects to that list , the list will not remain empty !!
when it comes the time to use the myObjectList in another class it returns an empty list with 0 length
class SomeClass {
.....
void getMyObjectsList(){
.....
// this display 0 length and has no data
print("my static object list length == ${MyData.myObjectList.length}");
}
}
i have also tried using a singleton instance and it return the same result :
class MyData {
static final MyData _singleton = MyData._internal();
factory MyData() {
return _singleton;
}
MyData._internal();
// object property
var List<MyObject> myObjectList = new List<MyObject>();
}
well this also return the default value which is an empty list:
class SomeClass {
.....
void getMyObjectsList(){
....
// this display 0 length and has no data
print("my singleton object list length == ${MyData().myObjectList.length}");
}
}
I think you missed to Store/Copy/Add objects in the List
class MyData {
// initialize it to an empty list
static List<MyObject> myObjectList = [];
}
Add some Objects to List before accessing the List
class SomeClass {
.....
/// Add Object to List using this Method
void addDataToList(MyObject data){
myObjectList.add(data);
}
void getMyObjectsList(){
.....
// this display 0 length and has no data
print("my static object list length == ${MyData.myObjectList.length}");
}
}
Note : if there is no Data list will return 0
so please add some data before using the .length() on List.
Hope this Solve your Issue
the problem was not in the static list , it was inside the TextField onTap event ,
i was using the static list inside it so i tried to use a local variable and i faced the same problem , the variable return the default value always.
Activity 1 sends the info I want to Activity 2:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id)
{
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
final int item_id = cursor.getInt(cursor.getColumnIndex(GamesDbAdapter.KEY_ROWID));
String item_cursus = cursor.getString(cursor.getColumnIndex(GamesDbAdapter.KEY_CURSUS));
String item_onderdeel = cursor.getString(cursor.getColumnIndex(GamesDbAdapter.KEY_ONDERDEEL));
String item_tijd = cursor.getString(cursor.getColumnIndex(GamesDbAdapter.KEY_TIJD));
String item_game = cursor.getString(cursor.getColumnIndex(GamesDbAdapter.KEY_GAME));
String item_web = cursor.getString(cursor.getColumnIndex(GamesDbAdapter.KEY_WEB));
Intent intent = new Intent(actGames_FRONT_ListViewCursorAdaptorActivity.this, actGames_ALL_ListViewCursorAdaptorActivity.class);
intent.putExtra("cursus", item_cursus);
intent.putExtra("id", item_id);
intent.putExtra("game", item_game);
startActivity(intent);
Toast done =
Toast.makeText(getApplicationContext(), item_game, Toast.LENGTH_LONG);
done.show();
}
});
In Activity2 I want to display "game" in another listview, but setText doesn't work with Listview.
game = (TextView)findViewById(R.id.game);
Intent iin= getIntent();
Bundle c = iin.getExtras();
if(c!=null)
{
String item_game =(String) c.get("game");
game.setText(item_game);
}
String game_list = getIntent().getExtras().getString("game");
This is the simplecursoradapter in activity2:
private void displayListView() {
Cursor cursor = dbHelper.fetchAllCursus();
// The desired columns to be bound
String[] cursus = new String[] {
GamesDbAdapter.KEY_CURSUS,
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.front,
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.games_row_front,
cursor,
cursus,
to,
0);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
And this is in my DBadapter:
public Cursor fetchGames(){
String[] args = {"********************" };
Cursor mCursor = mDb.query(SQLITE_TABLE, null,
"cursus = ?" , args, KEY_GAME, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
When I replace the *** with a static item from the database it works but I can't get it to work with the intent from the first activity. Is there a way to fill a listview with info from another listview in another activity?
I have a GWT view from which I grab the value of a dropdown and store it in a DB. The dropdown has the values "one" "two" "three". When I go back to the same view and I have "Two" stored in the DB then I want "Two" to be the selected item. However the only way I can get this to work at the moment is by iterating through each item in the listbox to find the one which matches and then set this as the selected one. Is there a better way to achieve this? I don't want to have to save the selected index.
I recommend you to extend ListBox and implement TakesValue interface. And in this class maintain a list variable which holds all the items in the ListBox. setValue and getValue should looks like the following code snippet -
private List<String> listItems = new ArrayList<String>();
public class MyListBox extends ListBox implements TakesValue<String>
{
public void setValue( String value )
{
if ( listItems.size() > 0 )
{
int valueIndex = 0;
if ( listItems.contains( value ) )
{
valueIndex = listItems.indexOf( value );
this.value = value;
}
setItemSelected( valueIndex, true );
}
}
public String getValue()
{
int selectedIndex = super.getSelectedIndex();
String value = null;
if ( selectedIndex >= 0 )
{
value = super.getValue( selectedIndex );
if ( "null".equals( value ) )
{
value = null;
}
}
return value;
}
public void setOptions(List<String> options)
{
listItems.clear();
listItems.addAll( items );
for ( String item : listItems )
{
addItem( item, item );
}
}
}
Now its just a matter of doing listBox.setValue( value ) method call from the view java file. Prior to this options must be set.
I have a cell table with an async data provider. If I update the data via the data provider the table renders the new data correctly but the selection model still holds onto and returns old objects.
Any ideas how to refresh the selection model?
I think you should make your SelectionModel work with different instance of the same "logical" object using the appropriate ProvidesKey. For instance, you could use ProvidesKey that calls getId on the object, so that two objects with the same such ID would be considered equal; so even if the SelectionModel holds onto the old object, it can still answer "yes, it's selected" when you give it the new object.
FYI, this is exactly what the EntityProxyKeyProvider does (using the stableId of the proxy). And the SimpleKeyProvider, used by default when you don't specify one, uses the object itself as its key.
I came across the same issue. Currently I have this as single selection model.
SelectedRow = store it when you select it.
Then when data is reloaded you can clear it by
celltable.getSelectionModel().setSelected(SelectedRow, false);
I guess it is too late for you but hope it helps someone else.
Here is my manual method for refreshing the SelectionModel. This allows you to use the selectedSet() when needed and it will actually contain the current data, rather than the old data - including the removal of deleted rows and updated fields!
I have included bits & pieces of a class extending DataGrid. This should have all the logic at least to solve your problems.
When a row is selected, call saveSelectionKeys().
When the grid data is altered call refeshSelectedSet().
If you know the key type, you can replace the isSameKey() method with something easier to deal with. This class uses generics, so this method attempts to figure out the object conversion itself.
.
public abstract class AsyncDataGrid<T> extends DataGrid<T> {
...
private MultiSelectionModel<T> selectionModel_;
private ListDataProvider<T> dataProvider_;
private List<T> dataList_;
private Set<Object> priorSelectionKeySet_;
private boolean canCompareKeys_;
...
public AsyncDataGrid( final ProvidesKey<T> keyProvider ){
super( keyProvider );
...
dataProvider_ = new ListDataProvider<T>();
dataList_ = dataProvider_.getList();
canCompareKeys_ = true;
...
}
private void saveSelectionKeys(){
priorSelectionKeySet_ = new HashSet<Object>();
Set<T> selectedSet = selectionModel_.getSelectedSet();
for( Iterator<T> it = selectedSet.iterator(); it.hasNext(); ) {
priorSelectionKeySet_.add( super.getValueKey( it.next() ) );
}
}
private void refeshSelectedSet(){
selectionModel_.clear();
if( priorSelectionKeySet_ != null ){
if( !canCompareKeys_ ) return;
for( Iterator<Object> keyIt = priorSelectionKeySet_.iterator(); keyIt.hasNext(); ) {
Object priorKey = keyIt.next();
for( Iterator<T> it = dataList_.iterator(); it.hasNext(); ) {
T row = it.next();
Object rowKey = super.getValueKey( row );
if( isSameKey( rowKey, priorKey ) ) selectionModel_.setSelected( row, true );
}
}
}
}
private boolean isSameRowKey( final T row1, final T row2 ) {
if( (row1 == null) || (row2 == null) ) return false;
Object key1 = super.getValueKey( row1 );
Object key2 = super.getValueKey( row2 );
return isSameKey( key1, key2 );
}
private boolean isSameKey( final Object key1, final Object key2 ){
if( (key1 == null) || (key1 == null) ) return false;
if( key1 instanceof Integer ){
return ( ((Integer) key1) - ((Integer) key2) == 0 );
}
else if( key1 instanceof Long ){
return ( ((Long) key1) - ((Long) key2) == 0 );
}
else if( key1 instanceof String ){
return ( ((String) key1).equals( ((String) key2) ) );
}
canCompareKeys_ = false;
return false;
}
}
I fixed my particular issue by using the following code to return the visible selection. It uses the selection model to determine what is selected and combines this with what is visible. The objects themselves are returned from the CellTable data which is always upto date if the data has ever been changed via an async provider (the selection model data maybe stale but the keys will be correct)
public Set<T> getVisibleSelection() {
/*
* 1) the selection model contains selection that can span multiple pages -
* we want to return just the visible selection
* 2) return the object from the cellTable and NOT the selection - the
* selection may have old, stale, objects if the data has been updated
* since the selection was made
*/
Set<Object> selectedSet = getKeys(selectionModel.getSelectedSet());
List<T> visibleSet = cellTable.getVisibleItems();
Set<T> visibleSelectionSet = new HashSet<T>();
for (T visible : visibleSet) {
if (selectedSet.contains(KEY_PROVIDER.getKey(visible))) {
visibleSelectionSet.add(visible);
}
}
return visibleSelectionSet;
}
public static Set<Object> getKeys(Collection<T> objects) {
Set<Object> ids = new HashSet<Object>();
for (T object : objects) {
ids.add(KEY_PROVIDER.getKey(object));
}
return ids;
}