How can I open a new activity on an expandable listview clicked - android-listview

How can I open a new activity on an expandable listview clicked
Am really desperate I need help I have been searching for months. I will be very glad if anyone helps.

New activities in Android can be started using Intents.
Now, it depends entirely on your ExpandableListView to decide which activity is opened. For instance, if the position is a factor, then you can do something like:
yourExpandableListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position == 1){
startActivity(new Intent(this, FirstActivity.class);
}else{
startActivity(new Intent(this, SecondActivity.class);
}
}
});

Related

What is the right usage for the SingleSelectionModel?

we would like to link from a CellTable to a property editor page. We use the SingleSelectionModel to get notified, when a user clicks on an item.
It is initialized like this:
private final SingleSelectionModel<Device> selectionModel = new SingleSelectionModel<Device>();
We then assign the selection change handler:
selectionModel.addSelectionChangeHandler(this);
Our selection change handler looks like this:
#Override
public void onSelectionChange(SelectionChangeEvent event) {
Log.debug("DevicesPresenter: SelectionChangeEvent caught.");
Device selectedDevice = selectionModel.getSelectedObject();
if (selectedDevice != null) {
selectionModel.clear();
if (selectionModel.getSelectedObject() != null){
Log.debug("DevicesPresenter: selected item is " + selectionModel.getSelectedObject());
}
else{
Log.debug("DevicesPresenter: selected item is null");
}
deviceEditorDialog.setCurrentDevice(selectedDevice.getUuid());
// get the container data for this device
clientModelProvider.fetchContainersForDevice(selectedDevice.getUuid());
PlaceRequest request = new PlaceRequest.Builder()
.nameToken(NameTokens.deviceInfo)
.with("uuid", selectedDevice.getUuid())
.build();
Log.debug("Navigating to " + request.toString());
placeManager.revealPlace(request);
}
}
Now there are two issues: There always seem to be two SelectionChangeEvents at once and i really cannot see why. The other thing is: How is the right way do handle selection of items and the related clearing of the selection model? Do we do that the right way?
Thanks!
If you only want to get notified of "clicks" without keeping the "clicked" item selected, use a NoSelectionModel instead; no need to clear the selection model as soon as something is selected.
As for your other issue with being called twice, double-check that you haven't added your selection handler twice (if you can unit-test your DevicesPresenter, introspect the handlers inside the selection model for example)
In your line selectionModel.addSelectionChangeHandler(this); what does this refer?
Here my code how I use SingleSelectionModel
public class MyClass{
private final SingleSelectionModel<CountryDto> selectionModel = new SingleSelectionModel<CountryDto>();
...
public MyClass(){
cellTable.setSelectionModel(selectionModel);
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
#Override
public void onSelectionChange(SelectionChangeEvent event) {
CountryDto selected = selectionModel
.getSelectedObject();
if (selected != null) {
Window.alert("Selected country "+selected.getTitle());
}
}
});
}
}

onActivityResult never fires unless I use getActivity() when calling startActivityForResult from a Fragment

My main activity opens a dialog fragment with 2 items in a listview. Clicking either one starts a new Activity. Unless I use getActivity().startActivityForResult() my code for onActivityResult never runs. Everything I've read here discourages using getActivity().startActivityForResult() and says just use startActivityForResult(). Normally I'd say "doesn't matter, code works" but its driving me nuts why its discouraged so much and why it won't work without getActivity(). I've been pouring over documentation and can't find an answer, help me stackoverflow, you're my only hope.
My onActivityResult() code located in my main activity (Landing.class):
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
String s = "";
Session current = new Session();
Gson gson = new Gson();
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
s = data.getStringExtra("SESSION_JSON");
current = gson.fromJson(s, Session.class);
}
}
sessions.add(current);
adapter.notifyDataSetChanged();
}
Code that calls startActivityForResult() located in my DialogFragment class:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
dismiss();
if (position == 0) {
Intent intent = new Intent(getActivity(), ActiveSessionActivity.class);
getActivity().startActivityForResult(intent, 1);
}
}
Code in ActiveSessionActivity class that should be returning the result to onActivityResult() in my main activity:
public void saveSession(View v) {
Session session;
Gson gson = new Gson();
String json = gson.toJson(session);
Intent intent = new Intent();
intent.putExtra("SESSION_JSON", json);
setResult(RESULT_OK, intent);
finish();
}
Android DialogFragments are still fragments, as such calling startActivityForResult from your dialog will actually be getting the result in the dialog. If you were to implement onActivityResult in your DialogFragment you'll get your callback. The reason getActivity().startActivityForResult() is discouraged is because the dialog has no control of the activity and it might not be attached anymore. Try...
if (getActivity() != null && !isDetached() && !isRemoving()) {
getActivity().startActivityForResult(...);
}

Cannot add ui widget to Eclipse RCP view on mousedown event

I'm trying to add new widgets on an RPC view by clicking on an existing button. The code that I'm using is the following:
public void createPartControl(final Composite parent) {
parent.setLayout(new RowLayout(SWT.HORIZONTAL));
Button btnNewButton = new Button(parent, SWT.NONE);
btnNewButton.setText("New Button");
btnNewButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseDown(MouseEvent e) {
Button b=new Button(parent,SWT.BUTTON1);
b.setText("asdasd");
}
});
}
The buttons are getting added on the view but are not visible. If I resize the view then they become visible. Why is this happening and how can it be solved?
I need somehow to refresh the view or call the event that the resize action calls.
The attached code works without problems in standard java applications.
Thank you,
Nick
Call the layout method of your parent Composite when you add a widget:
btnNewButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseDown(MouseEvent e) {
Button b=new Button(parent,SWT.BUTTON1);
b.setText("asdasd");
parent.layout();
}
});

Gwt form question

I have a gwt form which has about 70-100 widgets (textboxes,listboxes,custom widgets etc)
I am trying to implement the features of CUT ,COPY in this form .For this i have 2 buttons right on top of the form.
Now the problem i have is that when i click on the copy button , the widget that was focused in the form looses focus and i dont know which text to copy(or which widget was last focused before the focus getting to the copy button)
I was planning to implement blur handlers on all the widgets but i feel is a very laborious and not a good solution.
How can i get around this issue?
Thanks
Perhaps someone with a deeper insight might provide a better approach but I beleive adding blur handlers is perfectly valid. I do not quite see why you think it would be laborious, after all you don't need a different handler for each of your widgets, you can get away with only one(at most a couple for a variety of controls..), here is a very simple example,
public class CustomBlurHandler implements BlurHandler{
Object lastSource;
String text;
#Override
public void onBlur(BlurEvent event) {
if (event.getSource() instanceof TextBox) {
lastSource = event.getSource();
text = textBox.getSelectedText();
}
}
public Object getLastSource() {
return lastSource;
}
public String getText() {
return text;
}
}
and onModuleLoad :
public class Test implements EntryPoint {
CustomBlurHandler handler = new CustomBlurHandler();
public void onModuleLoad() {
TextBox text1 = new TextBox();
TextBox text2 = new TextBox();
text1.addBlurHandler(handler);
text2.addBlurHandler(handler);
Button b = new Button("Get last selected text");
b.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
Window.alert(handler.getLastSource()+ " " + handler.getText());
}
});
RootPanel.get().add(text1);
RootPanel.get().add(text2);
RootPanel.get().add(b);
}
}

DialogBox in GWT isn't draggable or centred

I'm new to GWT programming. So far I have a DialogBox which is supposed to collect a login and a password, which can if required launch another DialogBox that allows someone to create a new account.
The first of these two DialogBoxes always appears at the top left of the browser screen, and can't be dragged, although part of the definition of a DialogBox is that it can be dragged. However, the second DialogBox can be dragged about the screen without any problem.
What I'd really like is for the first DialogBox to appear in the middle of the screen & be draggable, both of which I thought would happen automatically, but there's not.
So, what things can stop a DialogBox from being draggable? There is nothing on the RootPanel yet. Does that make a difference?
Code fragments available if they help, but perhaps this general outline is enough for some pointers.
Thanks
Neil
Use dialogBox.center() This will center your DialogBox in the middle of the screen. Normally a DialogBox is by default draggable.
Just tried it out and it doens't matter if your RootPanel is empty our not. When I just show the DialogBox on ModuleLoad it is draggable and it is centered. Probably the problem is situated somewhere else.
This is the example of google itself:
public class DialogBoxExample implements EntryPoint, ClickListener {
private static class MyDialog extends DialogBox {
public MyDialog() {
// Set the dialog box's caption.
setText("My First Dialog");
// DialogBox is a SimplePanel, so you have to set its widget property to
// whatever you want its contents to be.
Button ok = new Button("OK");
ok.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
MyDialog.this.hide();
}
});
setWidget(ok);
}
}
public void onModuleLoad() {
Button b = new Button("Click me");
b.addClickListener(this);
RootPanel.get().add(b);
}
public void onClick(Widget sender) {
// Instantiate the dialog box and show it.
new MyDialog().show();
}
}
Here more information about the DialogBox.
Without seeing any of your code it's hard to tell what's going wrong. The following code works for me (ignore the missing styling...):
public void onModuleLoad() {
FlowPanel login = new FlowPanel();
Button create = new Button("create");
login.add(new TextBox());
login.add(new TextBox());
login.add(create);
create.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
final DialogBox box = new DialogBox();
FlowPanel panel = new FlowPanel();
Button close = new Button("close");
close.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
box.hide();
}
});
panel.add(new Label("some content"));
panel.add(close);
box.setWidget(panel);
box.center();
}
});
DialogBox firstBox = new DialogBox(false, true);
firstBox.setWidget(login);
firstBox.center();
}
Both boxes are draggable and shown in the center of your browser window.
Looks like you're overriding this method in Widget:
public void fireEvent(GwtEvent<?> event) {
if (handlerManager != null) {
handlerManager.fireEvent(event);
}
}
In Widget, handlerManager refers to a private HandlerManager.
Either add super.fireEvent(event) to your method or as you have done rename it.
Well, with vast amounts of trial and error I have found the problem, which was just this: I had a method in an object I'd based on DialogBox called fireEvent, which looked like this:
public void fireEvent(GwtEvent<?> event)
{
handlerManager.fireEvent(event);
}
Then, when a button was clicked on the DialogBox, an event would be created and sent off to the handlerManager to be fired properly.
And it turns out that if I change it to this (LoginEvent is a custom-built event):
public void fireEvent(LoginEvent event)
{
handlerManager.fireEvent(event);
}
... or to this ....
public void fireAnEvent(GwtEvent<?> event)
{
handlerManager.fireEvent(event);
}
the DialogBox is draggable. However, if the method begins with the line
public void fireEvent(GwtEvent<?> event)
then the result is a DialogBox which can't be dragged.
I'm a bit unsettled by this, because I can't fathom a reason why my choice of name of a method should affect the draggability of a DialogBox, or why using a base class (GwtEvent) instead of a custom class that extends it should affect the draggability. And I suspect there are dozens of similar pitfalls for a naive novice like me.
(Expecting the DialogBox to centre itself was simply my mistake.)