how to maintain list item selected color untill another item is not clicked - listitem

i have set values in my list item like
SimpleAdapter adapter = new SimpleAdapter(
this,
mListData,
R.layout.listrow,
new String[] { "text", "img" },
new int[] { R.id.text1, R.id.img } );
mLV = (ListView)findViewById(R.id.list2);
mLV.setAdapter(adapter);
mLV.setTextFilterEnabled(true);
mLV.setOnItemClickListener(listItemClickListener);
and my ListItemClickListner is like
AdapterView.OnItemClickListener listItemClickListener = new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View v, int arg2,
long arg3) {
System.gc();
// int id = mActivity.getResources().getIdentifier(mViews.get(arg2), "drawable", mActivity.getPackageName());
mView = mViews.get(arg2);
String str=mTitles.get(arg2);
Log.i("title", "tit"+str);
i am working with android tablet and i am not able to keep selected list item with diff color any know how to do this.

use custom list adapter extending BaseAdapter and there in getView() set the color of your selected item.

Related

Vaadin-Unable to update grid container on combobox value change

I am using vaadin 7.7.7
In a grid i have a combobox as an edited item in one of the columns
as
grid.addColumn("columnProperty").setEditorField(combobox);
I need to update a property/cell in same row based on the combobox selection change
My issue is , the selection change event triggers twice, once when the combobox in clicked and second when the selection value is changed. But the updated value in next cell gets reflected on UI only first time.
Below is the code written . Any solutions?
Combobox.addValueChangeListener(new ValueChangeListener()
#Override
public void valueChange(ValueChangeEvent event) {
// below line works only first time when the combobox is clicked,but i want
//it when the item in the combobox is changed
gridContainer.getContainerProperty(editedRow,"editedColumProperty").setValue("ValueTobeUpdated");}
});
Need to update the unit column on combobox change in edited mode(before saving)
Refer below link for image
example image
You will get value change events even when the field gets value that it should show to the user. In order to get event that indicates that the user has accepted the input you should use field group (setEditorFieldGroup).
From Book of Vaadin example for grid editing:
grid.getColumn("name").setEditorField(nameEditor);
FieldGroup fieldGroup = new FieldGroup();
grid.setEditorFieldGroup(fieldGroup);
fieldGroup.addCommitHandler(new CommitHandler() {
private static final long serialVersionUID = -8378742499490422335L;
#Override
public void preCommit(CommitEvent commitEvent)
throws CommitException {
}
#Override
public void postCommit(CommitEvent commitEvent)
throws CommitException {
Notification.show("Saved successfully");
}
});
Edit
I assume that you want to connect Parameter and Unit comboboxes. I would do that with this kind of value change lister
BeanItemContainer container = new BeanItemContainer<>(
Measurement.class,
measurements);
Grid grid = new Grid(container);
grid.setEditorEnabled(true);
ComboBox parameterComboBox = new ComboBox();
ComboBox unitComboBox = new ComboBox();
parameterComboBox.addItems(Parameter.Pressure, Parameter.Temperature, Parameter.Time);
parameterComboBox.addValueChangeListener(v -> setUnits(parameterComboBox, unitComboBox));
grid.getColumn("parameter").setEditorField(parameterComboBox);
grid.getColumn("unit").setEditorField(unitComboBox);
Units could be updated like this. I think you need to preserve current value and set it back if you replace available items in the combobox.
private void setUnits(ComboBox parameterComboBox, ComboBox unitComboBox) {
Object currentValue = unitComboBox.getValue();
List<String> units = unitsForParameter(parameterComboBox.getValue());
unitComboBox.removeAllItems();
unitComboBox.addItems(units);
if (units.contains(currentValue)) {
unitComboBox.setValue(currentValue);
} else {
unitComboBox.setValue(null);
}
}
private List<String> unitsForParameter(Object value) {
if (value == null) {
return Collections.emptyList();
} else if (value == Parameter.Pressure) {
return asList("Pascal", "Bar");
} else if (value == Parameter.Temperature) {
return asList("Celcius", "Kelvin");
} else if (value == Parameter.Time) {
return asList("Second", "Minute");
} else {
throw new IllegalArgumentException("Unhandled value: " + value);
}
}

Codenameone list adding functionality

I'm currently busy with a Codenameone app that requires me to add a list of items by button click like how you would add a task in a task list. How would I approach this? I'm a bit new at this. Please help.
This is a short example:
Form form = new Form("List Example"); //Create Form
Button button = new Button("PRESS ME"); //Create Button
form.add(button); // add button to Form
List myList = new List<>(); //Create List
form.add(myList); //add List to Form
// Create an Array of Elements
ArrayList<String> arrayList = new ArrayList<>();
for (int i = 0; i < 20; i++)
{
arrayList.add("Elemnt " + i);
}
// Create ListModel
DefaultListModel<String> listModel = new DefaultListModel<>(arrayList);
// Add Button ActionListner
button.addActionListener(new ActionListener<ActionEvent>()
{
public void actionPerformed(ActionEvent arg0)
{
myList.setModel(listModel); //add ListModel to List
form.repaint();
}
});
form.show();

Adding a popup Menu to a tree in e4 Rcp

I am creating a tree structure in RCP application. I want to able to create a pop up menu. I have been able to create a dummy menu item.
final Menu treeMenu = new Menu(check.getShell(), SWT.POP_UP);
MenuItem item = new MenuItem(treeMenu, SWT.PUSH);
item.setText("Open");
item.addSelectionListener(new SelectionListener() {
#Override
public void widgetSelected(SelectionEvent e) {
System.out.println("CAme in Open");
}
#Override
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
});
check.setMenu(treeMenu);
this menu however does not recognize the node details. I want something that can obtain information about the node which we have opened context menu on.
If you are using a TreeViewer (or TableViewer) just get the current selection:
IStructuredSelection sel = (IStructuredSelection)treeViewer.getSelection();
Object selectedElement = sel.getFirstElement();
For a Tree use:
TreeItem [] selectedItems = tree.getSelection();

JavaFx 8: update the current selected item of a ComboBox if it is modified

I have a ComboBox with a ObservableList<Item> as model. I can update, externally to it, the elements of the list and I want to update the current selected item of ComboBox if it's modified.
I tried to do:
private final ObservableList<Item> list;
private ComboBox<Item> comboBox;
....
comboBox.setItems(list);
comboBox.getSelectionModel().selectFirst();
System.out.println("selected index: " + comboBox.getSelectionModel().getSelectedIndex(); );
ListChangeListener<Item> listener = new ListChangeListener<Item>() {
#Override
public void onChanged(ListChangeListener.Change<? extends Item> c) {
while (c.next()) {
for (int i = c.getFrom(); i < c.getTo(); ++i) {
int selectedIndex = comboBox.getSelectionModel().getSelectedIndex();
System.out.println("selected index: " + selectedIndex );
if (i == selectedIndex) {
comboBox.getSelectionModel().select(selectedIndex);
}
}
}
}
};
list.addListener(listener);
I added a ChangeListener to the list, so when some element in the list is updated, I can update the selected item in the comboBox. But this doesn't work 'cause comboBox.getSelectionModel().getSelectedIndex() inside onChange returns me -1 and I don't understand why (If I print the value out of onChange, for example the first System.out, the value is correct).
Can you explain me this behavior and if there is a better way to achieve what I want?
Thanks.

AWT repaint issues

I am trying to make some small additions to some old java code that does not support swing. I need to add a small dialog that contains a panel which has a checkbox and a couple text fields. When the user clicks on the checkbox I want to disable or enable the checkboxes. This part seems to work well but the text fields are not properly getting redrawn. When I click the checkbox the fields do not appear to become enabled but if I then click on the panel or the text field you see that they are enabled (the opposite is also true, when I un-check the checkbox the fields still look enabled until you try and click on them and they become ghosted and do not become selected). I use the setEnabled(boolean) to set the status of the fields. I have tried calling repaint and validate on both the fields and the panel after changing the status and this does not seem to work. I have also tried to have the fields request focus and this did not work. Anyone have any other ideas?
//The class that contains all of this is of type Window
//Declaration of the components
private Panel _inputPanel;
private TextField min , max;
//This method adds to two text fields
public void addMinMaxtextFields(String min, String max) {
TextField minField = new TextField(min);
TextField maxField = new TextField(max);
this.min = minField;
this.max = maxField;
this.min.setEnabled(false);
this.max.setEnabled(false);
_inputPanel.add(minField);
_inputPanel.add(maxField);
}
//listener for the checkbox
public void itemStateChanged(ItemEvent e) {
Component[] components = _inputPanel.getComponents();
min.setEnabled(!min.isEnabled());
min.setVisible(true);
min.validate();
min.repaint();
_inputPanel.validate();
_inputPanel.repaint();
this.pack();
this.setSize(this.getWidth(), this.getHeight());
this.validate();
this.repaint();
/* do nothing */
}
You will need to call update(Graphics g) on Panel after setEnabled(boolean) is called.
check :
http://download-llnw.oracle.com/javase/1.4.2/docs/api/java/awt/Container.html#update(java.awt.Graphics)
I tried following code (built from code you provided), Its working fine.
import java.awt.Checkbox;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class CheckUI extends Dialog implements ItemListener {
// The class that contains all of this is of type Window
// Declaration of the components
private Panel _inputPanel;
private TextField min, max;
private Checkbox cb;
public CheckUI(Frame owner, boolean modal) {
super(owner, modal);
_inputPanel = new Panel();
this.add(_inputPanel);
addMinMaxtextFields("min", "max");
}
// This method adds to two text fields
public void addMinMaxtextFields(String min, String max) {
cb = new Checkbox();
cb.addItemListener(this);
TextField minField = new TextField(min);
TextField maxField = new TextField(max);
this.min = minField;
this.max = maxField;
this.min.setEnabled(false);
this.max.setEnabled(false);
_inputPanel.add(minField);
_inputPanel.add(maxField);
_inputPanel.add(cb);
}
// listener for the checkbox
public void itemStateChanged(ItemEvent e) {
Component[] components = _inputPanel.getComponents();
min.setEnabled(!min.isEnabled());
min.setVisible(true);
min.validate();
min.repaint();
_inputPanel.validate();
_inputPanel.repaint();
this.pack();
this.setSize(this.getWidth(), this.getHeight());
this.validate();
this.repaint();
/* do nothing */
}
/**
* #param args
*/
public static void main(String[] args) {
Frame parent = new Frame();
parent.setVisible(true);
parent.setExtendedState(Frame.MAXIMIZED_BOTH);
parent.pack();
CheckUI ui = new CheckUI(parent, true);
ui.pack();
ui.setVisible(true);
}
}

Resources