Display Field for select item in the list grid - gwt

Hello all
I am using smart gwt 2.2 on windows with mozilla browser.
I am using a list grid with two fields.
I set the editor type of second field to SelectItem.
In that select item I am rendering a grid.
For select item I set the value field and the display field.
But after the select any item from select item it is display value field.
I am also attaching the code for it.
code for grid
public void initializeGrid() {
ListGrid grid = new ListGrid();
setGridProperty(grid);
grid.setFields(getGridFields());
grid.setData(getGridData());
getSmartContainer().addChild(grid);
}
private static ListGridRecord[] getGridData() {
ListGridRecord record = new ListGridRecord();
record.setAttribute("id", "");
record.setAttribute("name", "");
ListGridRecord record1 = new ListGridRecord();
record1.setAttribute("id", "");
record1.setAttribute("name", "");
return new ListGridRecord[] { record, record1 };
}
private static void setGridProperty(ListGrid grid) {
grid.setWidth("90%");
grid.setHeight(125);
grid.setCanEdit(true);
grid.setEditEvent(ListGridEditEvent.CLICK);
grid.setEditByCell(true);
grid.setAlternateRecordStyles(true);
grid.setShowAllRecords(true);
}
private static ListGridField[] getGridFields() {
ListGridField field = new ListGridField("id");
FormItem item = new TextItem();
field.setEditorType(item);
ListGridField field1 = new ListGridField("name");
SelectItem item1 = new SelectItem("name");
setPropertyForSelectitem(item1);
DataSource dataSource = new DataSource();
dataSource.setClientOnly(true);
item1.setOptionDataSource(dataSource);
setDataForSelectItem(dataSource);
field1.setEditorType(item1);
return new ListGridField[] { field, field1 };
}
Code for select item
public static void setDataForSelectItem(DataSource dataSource) {
for (int i = 0; i < 2; i++) {
ListGridRecord record = new ListGridRecord();
record.setAttribute("id", "1");
record.setAttribute("name", "name" + i);
record.setAttribute("address", "address" + i);
dataSource.addData(record);
}
}
private static void setPropertyForSelectitem(SelectItem item) {
item.setPickListFields(getFieldsForSelectItem());
item.setWidth(150);
item.setValueField("id");
item.setDisplayField("name");
item.setPickListWidth(250);
}
private static ListGridField[] getFieldsForSelectItem() {
ListGridField idField = new ListGridField("id");
ListGridField nameField = new ListGridField("name");
ListGridField addField = new ListGridField("address");
return new ListGridField[] {idField,nameField,addField };
}
[/CODE]
After drawing this grid it is rendering value field for the select item, but i want to render the name (as it is the display field for the select item).
Same select item I used in the dynamic form but it is working well at that place, but for the grid it is not working well.
Please Help.
Thanks

Related

Is there a way to fill a listview in activity2 with info from a listview in activity1?

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?

Validate new row in SmartGWT ListGrid

I use ListGrid without datasource. And I use validator for some fields.
But then I add new record, it is not validated till I change value in new record.
I don't want to use grid.startEditingNew(), because if I don't change them, they do not have Record instance.
ListGrid grid = new ListGrid();
grid.setCanEdit(true);
ListGridField field1 = new ListGridField("attr1", "Name1");
ListGridField field2 = new ListGridField("attr2", "Name2");
CustomValidator validator = new CustomValidator() {
#Override
protected boolean condition(Object value) {
Logger.log("Validate: " + value);
return true;
}
};
field1.setValidators(validator);
field2.setValidators(validator);
grid.setFields(field1, field2);
Label addButton = new Label();
//...
addButton.setTooltip("Add");
addButton.addClickHandler(new ClickHandler() {
public void onClick(final ClickEvent clickEvent) {
ListGridRecord record = new ListGridRecord();
grid.addData(record);
// we can use grid.startEditingNew() instead
}
});
How I can force validation of the new row? Calling grid.validateRow(grid.getRecords().length - 1) do not lead to calling the validatior.
Why not validate values being set to the new Record instance (from FormItems?) instead?
You can reuse validators on ListGridField and FormItem.

How to implement something like a wizard screen?

I want to place a "Next" button which , when clicked , will display another group of components ; and I want also to place a "Previous" button which , when clicked , then display the previous group of components. How to achieve that ?
I recently implemented forms for data entry. Typically i have a wizard class that holds all the forms in the wizard, so i can easily navigate back and forth between them. And when i call a new form, i pass along the object of the wizard.
Below is my wizard, with implementation omitted.
public final class ReportWizard {
public static ReportWizard instance = null;
Form parent = null;
Form titleForm = null;
Form budgetForm = null;
Form iconForm = null;
final Report reports[] = new Report[20];
public ReportWizard(Form parent) {
this.parent = parent;
this.instance = this;
}
void getTitle() {
AddReportForm reportForm = new AddReportForm(parent, this);
reportForm.showReportForm();
titleForm = reportForm;
ImageListPicker getIcon = new ImageListPicker(titleForm, reports, this);
iconForm = getIcon.imageListForm;
}
void getIcon() {
iconForm.show();
}
public void cancelWizard() {
titleForm = null;
iconForm = null;
budgetForm = null;
instance = null;
parent.show();
parent = null;
System.gc();
}
}

Setting ListGrid selection in SmartGWT with method "selectRecords(Record record)"

I'm trying to set the selected records of a ListGrid table object in SmartGWT using records, but I can't find any way of doing it. I want to select with record, not index. I want to use selectRecord(Record record) method.
As an example;
public void onModuleLoad()
{
VLayout main = new VLayout();
final ListGrid grid = new ListGrid();
grid.setHeight(500);
grid.setWidth(400);
grid.setFields(new ListGridField("name", "Name"));
grid.setData(createRecords());
final IButton button = new IButton("Select some");
button.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event)
{
grid.selectRecord(createRecord("orange"));
}
});
main.addMember(grid);
main.addMember(button);
RootPanel.get().add(main);
}
private ListGridRecord[] createRecords()
{
return new ListGridRecord[]{
createRecord("monkey"),
createRecord("banana"),
createRecord("orange"),
createRecord("sun")
};
}
private ListGridRecord createRecord(String name)
{
ListGridRecord record = new ListGridRecord();
record.setAttribute("name", name);
return record;
}
In this case I want to select orange, But this code select anything.
Is it posible? If possible how?
Thanks in advance.
Found this solution;
selectRecord(grid.getRecordList().find("name", "orange"));
There's a problem with your code:
When you write
grid.selectRecord(record);
it goes to search the same record instance that the grid has. If both instances of record are equal, only then & then it selects the record. Otherwise nothing happens as you're facing right now. Here what you need to do is:
ListGridRecord[] records = countryGrid.getRecords();
int i;
for (i = 0; i < records.length; i++)
{
if (records[i].getAttribute("name").equalsIgnoreCase("orange"))
{
break;
}
}
countryGrid.selectRecord(i);

Setting ListGrid selection in SmartGWT

I'm trying to set the selected records of a ListGrid table object in SmartGWT, but I can't find any way of doing it. I know there's a getSelectedRecords() function, but no matching setSelectedRecords(). I tried to see if set/getSelectedState() would work, but GWT complains about needing a primary key and a DataSource object. Is there any way to set the selection of a ListGrid?
For this you can use one of the selectRecords() methods, like so:
public void onModuleLoad()
{
VLayout main = new VLayout();
final ListGrid grid = new ListGrid();
grid.setHeight(500);
grid.setWidth(400);
grid.setFields(new ListGridField("name", "Name"));
grid.setData(createRecords());
final IButton button = new IButton("Select some");
button.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event)
{
grid.selectRecords(new int[]{2, 3}); //This will select index 2 and 3
}
});
main.addMember(grid);
main.addMember(button);
RootPanel.get().add(main);
}
private ListGridRecord[] createRecords()
{
return new ListGridRecord[]{
createRecord("monkey"),
createRecord("banana"),
createRecord("orange"),
createRecord("sun")
};
}
private ListGridRecord createRecord(String name)
{
ListGridRecord record = new ListGridRecord();
record.setAttribute("name", name);
return record;
}