Android :
What I've implemented till now is, get spinner value to the immediate subscriber using itemSelection().
Code :
Observavle observavle = RxAdapterView.itemSelections(spinner);
observable.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(integer -> {
Log.v("spinner", integer.toString());
});
What I want to achieve is :
I want to subscribe multiple observers to this Observable.
For which I have created few Observers and used
Observable.subscribe(observer1);
Observable.subscribe(observer2);
Observable.subscribe(few more observers);
To get spinner updated value on these observers, but this is not working,
OnNext() of these observers won't get called on spinner value change.
(Note: This situation works perfectly in case of RxBinding Textview using on textchange() ).
Will surely upvote if you can help me with this.
You can use the share() operator:
Observavle observavle = RxAdapterView.itemSelections(spinner).share();
You can read more about share() in this blog post.
Related
first question here by a Kotlin and AndroidStudio noob.
I made an app and want to go from one activity to another. I use buttons to navigate from the MainScreen to the 2 different screens. In Main I have this:
val firstBTN: Button = findViewById (R.id.firstBTN)
firstBTN.setOnClickListener {
val intent = Intent(this, FirstActivity::class.java)
intent.putExtra("Collection", collection as Serializable)
intent.putExtra("Result", result)
startActivity(intent)
collection is a MutableListOf objects called Edibles (String, Double) that I want to give to the next activity
it is then received by this:
val intentE = getIntent()
var collection = intent.getSerializableExtra("Collection") as MutableList<Edibles>
var result = intent.getDoubleExtra("Result",0.0)
But everytime I press firstBTN the App stops working.Unfortunately I don't even get any bug report, so I have no idea what's the problem. I thought it might be the "as MutableList" part, but I even created an element before passing to the next activity and the same happened...
Thank you already for your help!
This could be anything. You should provide more data.
For starters your Android Manifest. Did you put your second activity in there?
Does the call work without the putExtra?
Second, what type is collection? Is the source data class annotated with #Serializable? I am not very sure that as Serializable alone will work because there are different Serializables.
https://kotlinlang.org/docs/serialization.html#example-json-serialization
When LogCat does not work, check about filters. If there are none and you see any reactions, then restart Android Studio. There are some Bugs which can stop LogCat from working.
Observablecollection is bound to gridview in UWP project. If I try to clear and add data it fails with an error because it can only be modified on the UI thread.
I have set up service broker with SQL to notify the app when there is a change to the data. This is working correctly. However, every time I try to clear and modify the observablecollection I get an exception thrown.
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
EmployeeLists.Add(new Employee { Name = dr[0].ToString(), Loc = dr[2].ToString() });
}
}
This is the code I'm using at first to populate the observable collection. I want to listen for changes which is working. But how do I update the changes and sync them to the observable collection?
I have tried clearing the employeelists observablecollection and then adding everything again. It seems clunky, but doesn't work anyway because It says I cannot modify from another thread. I have tried several solutions online, but I'm not that familiar with ASYNC programming. Can anyone point me in the right direction?!
I have some general questions, I start of on page main.ts
Via onInit I am grabbing data from a mongoose database server, which I save in an array.
This array needs to be available in other components. The way I do this right now, which seems to work, by using a service.ts
In the service I have a lot BehaviorSubjects and Subjects. So whenever I get the array data from mongoose I send a message to the service, and the other components subscribe to that message.
I am sometimes using Subject instead of BehaviorSubject because it throws error messages as in HTML I am using *ngFor and it expects an array, not a string 'default message'.
I am just wondering if this is a correct setup to move data between pages.
Right now I am also using this message system for updates on my component.
So if someone makes a comment or post on my website I send a message to my service which in turn updates an observable and my component subscribes again to that.
Are there better ways to update my site for new data and is there an easy way to explain why sometimes I get this error message from *ngFor and in other cases it doesn't throw this error whilst still using *ngFor.
i.e. when I update an observable will the component receive the message straight away or will it receive onInit.
With that also the question if it is best practice to use the below in the constructor of the component or the onInit.
I unsubscribe OnDestroy.
this.newService.currentMessageComment
.takeWhile(() => this.alive)
.subscribe(message => {
service.ts
public messageSourceMarketCap = new BehaviorSubject<any>('default message');
currentMessageMarketCap = this.messageSourceMarketCap.asObservable();
public messageSourceHistory = new Subject<any>();
currentMessageHistory = this.messageSourceHistory.asObservable();
public messageSouceApi = new BehaviorSubject<any>('default message');
currentMessageApi = this.messageSouceApi.asObservable();
public messageSourceBody = new BehaviorSubject<any>('default message');
currentMessageBody = this.messageSourceBody.asObservable();
Why are you using BehaviouSubject. I think in your case Subject will be better. BehaviouSubject fits in case when you want to pass a initial value while creating observable.
You can also quick fix by emiting an empty array as ngFor expects an array. Change from "default message" to [].
Hope it will help
I have a working paper with graph. I have added several cells to the graph and I'm trying to listen to the cell:highlight event but I never receive it.
I'm doing:
paper.on('cell:highlight', function() { ... });
Other events seem to work fine, for example: blank:pointerup,...
Is there something special to do to make cell events work ?
According to documentation:
cell:highlight - triggered when highlight() method is called on either
an element or a link. Note that this method is also called
automatically when the user is reconnecting a link and the connection
is valid (validateConnection() returns true) or if embeddingMode is
enabled on the paper and the dragging element is above another element
it could be dropped into (validateEmbedding() returns true). The
handler for this event has the following signature: function(cellView,el). The handler defaults to function(cellView, el) {
V(el).addClass('highlighted') }. In other words, the 'higlighted' CSS
class is added and so you can style the highlighted element in CSS. If
you want to use a different method for highlighting cells, call
paper.off('cell:highlight') first to unregister the default handler
and then paper.on('cell:highlight', myHandler) to register your own.
You can read more about it here.
I am writing a program which need to listen the user keyboard stroks.
I use function XGrabKeyboard() and this is my code:
XGrabKeyboard(pDisplay, DefaultRootWindow(pDisplay), True, GrabModeAsync, GrabModeAsync, CurrentTime);
XEvent event;
while (true)
{
XNextEvent(pDisplay, &event);
switch (event.type)
{
...
}
}
But it causes the keyboard and cursor to be frozen.
I looked up the man page, it only says: "The third parameter specifies a Boolean value that indicates whether the keyboard events are to be reported as usual."
I tried both true or false or the 3rd param, both GrabModeAsync and GrabModeSync for the 4th and 5th param, but it doesn't work.
After calling XGrabKeyboard(), the keyboard is frozen and mouse click doesn't response.
Any ideas?
XGrabKeyboard() (if successful - be sure to check the return value), redirects all key events to your client.
So if your "..." inside the while(true) does not properly handle those key events, or does not ever ungrab (XUngrabKeyboard) or release sync events (XAllowEvents, only applies to GrabModeSync), then the keyboard would appear to lock up.
The boolean parameter is owner_events which indicates whether to report key events always to the window provided to XGrabKeyboard, or report them to the window they normally would have gone to without the grab. Typically you want False (report to the grab window).
For typical uses of XGrabKeyboard (I don't know your use-case) the parameters you would want are:
grab window = some window in your app that relates to the reason for the grab
owner_events=False to send all events to that window
pointer_mode=Async to not screw with the pointer
keyboard_mode=Async to just redirect all key events and avoid need for AllowEvents
time=the timestamp from the event triggering the grab, ideally, or one generated by changing a property and grabbing the timestamp off the PropertyNotify
But, it depends. To give any definitive answer you'd probably need to post a compilable program, I think the bug is likely in the "..." part of your code. Try narrowing your app down to a single-file test case that can be run by others perhaps. Or explain more why you are grabbing and what you're trying to accomplish in the big picture.
I cant help with the XGrabKeyboard function - I havent used it before and dont know how it works - but I can suggest another way of getting the keyboard events.
When creating my window using XCreateWindow, the last argument is a XSetWindowAttributes object. This object has a member event_mask, which you can use to choose which events your window will receive.
I set mine like this:
XSetWindowAttributes setWindAttrs
setWindAttrs.event_mask = ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask;
That will mean you receive events for keyboard key presses and mouse button clicks if you pass this object to XCreateWindow on window creation.
Also another note you can use XPending(pDisplay) to check if there are still events waiting to be handled - so it could replace true in your while(true) line.
Edit: Also your freezing issue could be that you dont return false anywhere in your while loop? It may be stuck in an infinite loop, unless you just removed that bit for the post. Try replacing true with xpending as I suggested above and it may fix the issue, or just returning false after handling the event, but this would only handle one event per frame rather than handling all the currently pending events like XPending would do, and I assume that is what you want to do.