addPointerPressedListener not working on Table - codenameone

I have a table component in my form with lots of values. I want a pop up on clicking on each cell in the table to show some information. I wrote a code for that :
dataTable.addPointerPressedListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Label c = (Label) dataTable.getClosestComponentTo(evt.getX(), evt.getY());
Dialog.show("Artifact", information, "Ok", null);
}
});
This method only works for last row and last column only and also not picking the right cell (closest cell). What I want is to pick the right cell on clicking in the table. What are the alternatives for doing this ?
I tried another way to do so. Code is
#Override
public void addPointerPressedListener(ActionListener l) {
l = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Label c = (Label) getClosestComponentTo(evt.getX(), evt.getY());
Dialog.show("Popup", c.getText(), "Ok", null);
}
};
super.addPointerPressedListener(l);
}
Both way I am getting problem. Its working on outer cells and at specific points only.

The table is a Container in which each cell is a component in its own right. To track an individual cell you can override the createCell method in table and track the component returned from there.

Related

Why is the return value always 0 from getSelectedIndex from a DefaultListModel?

I have an AutoCompleteTextField working with a DefaultListModel.
options = new DefaultListModel<>();
labelACField = new AutoCompleteTextField(options){
#Override
protected boolean filter(String text) {
}
};
The return value from options.getSelectedIndex() always returns 0 from actionPerformed, even users choose other items in the AutoCompleteTextField.
labelACField.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
if (filteredIdObjs!=null){
selectedIdx = options.getSelectedIndex();
For example, a user clicks "VV Vanguard Large-Cap ETF" or IVOV....or IVVD, or CVV.. the return value from options.getSelectedIndex() always returns 0.
How do I get the item index that the user clicks?
That's an interface designed for a List. AutoCompleteTextField uses the ListModel interface for ease of use. But it doesn't use selection since it's inherently a text field.
The output is the text within the text field. Not list selection since a user can type in any arbitrary value without selecting a value from the list.

After items selection in my ListView other items get randomly selected while scrolling Kotlin

I'm new to this. After scrolling my ListView, items in "same position" as items I selected before get auto selected. (Same position I mean position in the screen not in the database.) I had this prob before, because I was selecting items by their indexes of ListView in OnItemClickListener. However i face this prob again although I'm doing it right way I think.
When I click on item in ListView, I get its unique ID and based on that I change SELECTED value of this item (of this row in the database) to 0 or 1 (depending on this if it was clicked or not). After that I switch backgrnd color to grey (or back to white). This is handled in CursorAdapter which distinguishes SELECTED property.
Here's my code.
OnCreate in MainActivity.kt
val dbHelper = DBHelper(this)
val db = dbHelper.writableDatabase
val myCursor = db.rawQuery("SELECT * FROM ${ContractClass.FeedReaderContract.TABLE_NAME}", null)
val myAdapter = CursorAdapter(this, myCursor)
myListView.adapter = myAdapter
myListView.setOnItemClickListener { _, view, _, _ ->
val text = view.txtName.text
val select = "${ContractClass.FeedReaderContract.COLUMN_NAME_ENWORD} MATCH ?"
val selectCursor = db.query(
ContractClass.FeedReaderContract.TABLE_NAME, // The table to query
null, // The array of columns to return (pass null to get all)
select, // The columns for the WHERE clause
arrayOf("$text"), // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
null // The sort order
)
with(selectCursor) {
while (moveToNext()) {
val itemSel = getInt(getColumnIndexOrThrow(ContractClass.FeedReaderContract.COLUMN_NAME_SELECTED))
if (itemSel == 1){
val values = ContentValues().apply {
put(ContractClass.FeedReaderContract.COLUMN_NAME_SELECTED, 0)
}
val count = db.update(
ContractClass.FeedReaderContract.TABLE_NAME, values, select, arrayOf("$text"))
}else{
val values = ContentValues().apply {
put(ContractClass.FeedReaderContract.COLUMN_NAME_SELECTED, 1)
}
val count = db.update(
ContractClass.FeedReaderContract.TABLE_NAME, values, select, arrayOf("$text"))
}
}
}
}
CursorAdapter.kt
class CursorAdapter(context: Context, cursor: Cursor) : CursorAdapter(context, cursor, 0) {
// The newView method is used to inflate a new view and return it,
// you don't bind any data to the view at this point.
override fun newView(context: Context, cursor: Cursor, parent: ViewGroup): View {
if (cursor.getInt(cursor.getColumnIndexOrThrow(ContractClass.FeedReaderContract.COLUMN_NAME_SELECTED)) == 0){
return LayoutInflater.from(context).inflate(R.layout.row_list_row, parent, false)
}else{
return LayoutInflater.from(context).inflate(R.layout.user_list_row_selected, parent, false)
}
}
// The bindView method is used to bind all data to a given view
// such as setting the text on a TextView.
override fun bindView(view: View, context: Context, cursor: Cursor) {
// Find fields to populate in inflated template
val tvBody = view.findViewById<View>(R.id.txtName) as TextView
val tvPriority = view.findViewById<View>(R.id.txtComment) as TextView
val tvPriority2 = view.findViewById<View>(R.id.txtThird) as TextView
val tvPriority3 = view.findViewById<View>(R.id.txtThi) as TextView
// Extract properties from cursor
val body = cursor.getString(cursor.getColumnIndexOrThrow(ContractClass.FeedReaderContract.COLUMN_NAME_ENWORD))
val priority = cursor.getString(cursor.getColumnIndexOrThrow(ContractClass.FeedReaderContract.COLUMN_NAME_DEFN))
val priority2 = cursor.getInt(cursor.getColumnIndexOrThrow(ContractClass.FeedReaderContract._id))
val priority3 = cursor.getInt(cursor.getColumnIndexOrThrow(ContractClass.FeedReaderContract.COLUMN_NAME_SELECTED))
// Populate fields with extracted properties
tvBody.text = body
tvPriority.text = priority.toString()
tvPriority2.text = priority2.toString()
tvPriority3.text = priority3.toString()
}
}
Database Table Create
private val SQL_CREATE_ENTRIES =
"CREATE VIRTUAL TABLE ${ContractClass.FeedReaderContract.TABLE_NAME} USING fts4(" +
"${ContractClass.FeedReaderContract._id}," +
"${ContractClass.FeedReaderContract.COLUMN_NAME_ENWORD} TEXT," +
"${ContractClass.FeedReaderContract.COLUMN_NAME_DEFN} TEXT," +
"${ContractClass.FeedReaderContract.COLUMN_NAME_SELECTED} INTEGER)"
I found similar post here: List view with simple cursor adapter items checked are un-checked during scroll
but I think, they suggest what I've already done.
Thanks for any help.
You don't appear to be changing the ListView's Cursor after you have made changes to/updated the underlying data.
Try, after making changes to the underlying data, updating the ListView's Cursor by
re-querying the data and then
using the Adapter's swapCursor(myCursor) (or the notiftDatasetChanged() method)
Here's an equivalent of your App but in Java rather than Kotlin (not having any luck converting as I hardly ever use Kotlin).
This I believe does what you want. That is,
If you select an unselected row then all rows that contain the enword are selected and greyed out and the selected column value is set to 1.
If you select a selected (grey) row then those rows that contain the enword are de-selected and are change to white with the selected column value being changed back to 0
Note that rather than create an FTS table I've mimicked the FTS and used LIKE instead of MATCH.
Are toggled if they are selected the background is grey else white. e.g. initially it is :-
If cat is clicked (2nd row) then all other cat rows are also toggled and greyed as per :-
and so on.
The Code
MainActivity (the file I had issues converting)
public class MainActivity extends AppCompatActivity {
DBHelper dbhelper;
ListView myListView;
MyCursorAdapter myAdapter;
Cursor mCursor;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myListView = this.findViewById(R.id.myListView);
dbhelper = new DBHelper(this);
addSomeTestingData();
manageListView();
}
private void manageListView() {
mCursor = dbhelper.getAllRows();
if (myAdapter == null) {
myAdapter = new MyCursorAdapter(this,mCursor);
myListView.setAdapter(myAdapter);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
dbhelper.updateSelected(mCursor.getString(mCursor.getColumnIndex(ContractClass.FeedReaderContract.COLUMN_NAME_ENWORD)));
manageListView();
}
});
} else {
myAdapter.swapCursor(mCursor);
}
}
private void addSomeTestingData() {
if (DatabaseUtils.queryNumEntries(dbhelper.getWritableDatabase(),ContractClass.FeedReaderContract.TABLENAME) > 1) return;
for (int i=0; i < 10; i++) {
dbhelper.addRow("Apple", "Thing that falls from trees");
dbhelper.addRow("Cat", "Something that is furry and sits on mats");
dbhelper.addRow("Bear", "Something that is furry that eats honey but doesn't ssit on a mat");
dbhelper.addRow("Dog", "Something is furry and friendly");
dbhelper.addRow("Echida", "An upside down hedgehog");
dbhelper.addRow("Ferret", "Something that is furry and found up trouser legs");
dbhelper.addRow("Giraffe", "Something that has 5 legs one pointing up");
dbhelper.addRow("Hippo", "An animal that loves mud and water but not humans");
dbhelper.addRow("Ibis", "A white feathered flying thing");
dbhelper.addRow("Jaguar", "A car or a large black cat");
dbhelper.addRow("Kangaroo", "A marsupial that boxes, skips and has a puch for shopping trips");
dbhelper.addRow("Lizard", "A rock dweller");
dbhelper.addRow("Mammoth", "A big hairy elephant now extinct");
dbhelper.addRow("Nuthatch", "A small bird that does not customise nuts so they have hatches.");
dbhelper.addRow("Ostrich", "A l argefast running bird that does not fly");
dbhelper.addRow("Panther", "A skinny pink cat that walks on only two of it's four lehs");
dbhelper.addRow("Queen", "A female rule of a country");
dbhelper.addRow("Rhinocerous", "A Hippo like animal that has a name that is hard to spell");
dbhelper.addRow("Tiger", "A live verion of Winnie the Pooh's friend Tigger");
dbhelper.addRow("Stork", "A skinny ostrich that flies and delivers children through Dream World.");
}
}
}
Obviously the addSomeTestingData method is just for that.
Note that there's hardly any other code. The DB access has all been moved to the DBHelper class.
The crux of the matter is the manageListView method.
First the Cursor used by the adapter is populated.
The Adapter hasn't been instantiated and is therefore null is instantiated and then tied to the ListView.
The OnItemClickListener is added noting that it calls the manageListView method after the database has been updated.
If the Adapter has been instantiated then the swapCursor method is called that tells the adapter that underlying cursor has been changed.
DBHelper.java
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(#Nullable Context context) {
super(context, ContractClass.DBNAME,null,ContractClass.DBVERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(ContractClass.FeedReaderContract.CRTSQL);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor getAllRows() {
SQLiteDatabase db = this.getWritableDatabase();
return db.query(ContractClass.FeedReaderContract.TABLENAME,null,null,null,null,null,null);
}
public void updateSelected(String selected) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("UPDATE "
+ ContractClass.FeedReaderContract.TABLENAME
+ " SET " + ContractClass.FeedReaderContract.COLUMN_NAME_SELECTED
+ "= CASE " + ContractClass.FeedReaderContract.COLUMN_NAME_SELECTED +
" WHEN 1 THEN 0 ELSE 1 END " +
" WHERE " + ContractClass.FeedReaderContract.COLUMN_NAME_ENWORD + " LIKE ?",
new String[]{selected}
);
}
public long addRow(String enWord, String definition) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(ContractClass.FeedReaderContract.COLUMN_NAME_ENWORD,enWord);
cv.put(ContractClass.FeedReaderContract.COLUMN_NAME_DEFN,definition);
return db.insert(ContractClass.FeedReaderContract.TABLENAME,null,cv);
}
}
Selecting all rows has been moved here as the getAllRows method
The addRow is just to allow some testing data to be added.
The updateSelected method, rather than extracting rows into a Cursor, to drive the updates instead uses a CASE WHEN ELSE END clause to toggle the value and should be more efficient.
Note that instead of MATCH as MATCH is FTS specififc (I believe) LIKE has been used as the underlying table is not an FTS table. You would use MATCH
MyCursorAdapter.java
public class MyCursorAdapter extends CursorAdapter {
public MyCursorAdapter(Context context, Cursor c) {
super(context, c, false);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.row_list_row,parent,false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
if (cursor.getInt(cursor.getColumnIndex(ContractClass.FeedReaderContract.COLUMN_NAME_SELECTED)) < 1) {
view.setBackgroundColor(0xFFF9F9F9);
} else {
view.setBackgroundColor(0xFFD9D9D9);
}
TextView tvBody = view.findViewById(R.id.txtName);
TextView tvPriority = view.findViewById(R.id.txtComment);
TextView tvPriority2 = view.findViewById(R.id.txtThird);
TextView tvPriority3 = view.findViewById(R.id.txtThi);
tvBody.setText(cursor.getString(cursor.getColumnIndex(ContractClass.FeedReaderContract.COLUMN_NAME_ENWORD)));
tvPriority.setText(cursor.getString(cursor.getColumnIndex(ContractClass.FeedReaderContract.COLUMN_NAME_DEFN)));
tvPriority2.setText(cursor.getString(cursor.getColumnIndex(ContractClass.FeedReaderContract._id)));
tvPriority3.setText(cursor.getString(cursor.getColumnIndex(ContractClass.FeedReaderContract.COLUMN_NAME_SELECTED)));
}
}
The main difference is that rather than juggle swapping layouts the background of the view is changed in the bindView method, rather than the newView method.
You need to override getItemViewType(position) if you want to use multiple different view types. If you don't, then the adapter has no way to know whether or not the View instance it is passing as convertView is of the right type, and you will wind up with badly-recycled views.
This doesn't appear to be trivial for CursorAdapter (with which I don't have any experience). I think the right way to do it should be something like this:
override fun getItemViewType(position: Int): Int {
cursor.moveToPosition(position)
val columnIndex = cursor.getColumnIndexOrThrow(ContractClass.FeedReaderContract.COLUMN_NAME_SELECTED)
return when (cursor.getInt(columnIndex)) {
0 -> 0
else -> 1
}
}
I think also that you should change newView() to leverage these types. Leaving what you have should work, but it would be duplicated code.
override fun newView(context: Context, cursor: Cursor, parent: ViewGroup): View {
val layoutId = when (val type = getItemViewType(cursor.position)) {
0 -> R.layout.row_list_row
1 -> R.layout.user_list_row_selected
else -> throw IllegalStateException("unexpected viewType: $type")
}
return LayoutInflater.from(context).inflate(layoutId, parent, false)
}

Extending WPF Calendar to support selecting many dates at once

I want to add a method to the WPF Calendar control to enable it to select many dates at once, and raise the SelectedDatesChangedEvent only once at the end.
The WPF Calendar control allows you to add only one date at a time (ranges are not useful to me). However I might need to add some 1000 dates and I don't want the SelectedDatesChangedEvent handler called 1000 times because in my case it's an expensive operation.
The WPF DataGrid has a very nice feature that allows for this to be done:
public class MyDataGrid : DataGrid
{
public void ClearAndSelectMany(IEnumerable<DateTime> datesToBeSelected)
{
this.BeginUpdateSelectedItems();
...
foreach (DateTime date in datesToBeSelected)
this.SelectedDates.Add(date);
...
this.EndUpdateSelectedItems();
}
}
However the Calendar doesn't have anything like DataGrid's BeginUpdateSelectedItems(), so I'm trying to create a workaround by preventing base.OnSelectedDatesChanged() being called until it's all done:
public class MyCalendar : Calendar
{
protected override void OnSelectedDatesChanged(SelectionChangedEventArgs e)
{
if (false == this.temporaryDontReportSelectionChanged)
base.OnSelectedDatesChanged(e); // this is where I get an exception
}
public void ClearAndSelectMany(IEnumerable<DateTime> datesToBeSelected)
{
this.temporaryDontReportSelectionChanged = true;
...
foreach (DateTime date in datesToBeSelected)
SelectedDates.Add(date);
...
this.temporaryDontReportSelectionChanged = false;
OnSelectedDatesChanged(
new SelectionChangedEventArgs(
MyCalendar.SelectedDatesChangedEvent,
removedDates.ToList(),
addedDates.ToList()));
}
}
Now my problem is that I'm getting an exception when calling base.OnSelectedDatesChanged():
Unable to cast object of type
'System.EventHandler`1[System.Windows.Controls.SelectionChangedEventArgs]'
to type
'System.Windows.Controls.SelectionChangedEventHandler'.
I suppose I didn't properly create the SelectionChangedEventArgs object near the end, but I have no idea how to do it right. Any help will be appreciated.
Update: Motivated by Jamleck's question, I recreated the problem in a new project, and now have a bit more information to provide. Here's the MyCalendar class:
public class MyCalendar : System.Windows.Controls.Calendar
{
private bool temporaryDontReportSelectionChanged;
public MyCalendar()
{
// removing this line below makes my problem go away and it works ok
this.SelectedDatesChanged += MyCalendar_SelectedDatesChanged;
}
void MyCalendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
{
}
protected override void OnSelectedDatesChanged(SelectionChangedEventArgs e)
{
if (!temporaryDontReportSelectionChanged)
base.OnSelectedDatesChanged(e);
}
public void ClearAndSelectMany(IEnumerable<DateTime> datesToBeSelected)
{
this.temporaryDontReportSelectionChanged = true;
...
foreach (DateTime date in datesToBeSelected)
SelectedDates.Add(date);
...
this.temporaryDontReportSelectionChanged = false;
OnSelectedDatesChanged(
new SelectionChangedEventArgs(
MyCalendar.SelectedDatesChangedEvent,
removedDates.ToList(),
addedDates.ToList()));
}
}
So if I don't add an event handler to the SelectedDatesChanged event handler, everything works great, but if I do add it, then I get the InvalidCastException described above.

Windows Form Combobox.Items.Clear() leaves empty slots

I am working on a Windows Form that has multiple comboboxes. Depending on what is chosen in the first combobox determines what items are filled into the second combobox. The issue I am running into is if I choose ChoiceA in ComboBox1, ComboBox2 is clear()ed, then is filled with ChoiceX, ChoiceY, and ChoiceZ. I then choose ChoiceB in ComboBox1, ComboBox2 is clear()ed, but there are no choices to add to ComboBox2, so it should remain empty. The issue is, after choosing ChoiceB, there's a big white box with three empty slots in ComboBox2. So, basically, however many items are cleared N, that's how many empty slots show up after choosing ChoiceB.
This might be a tad confusing, I hope I explained it well enough.
-- EDIT Adding Code, hope it helps clear things up. BTW, mainItemInfo is another "viewmodel" type class. It interfaces back into the form to make updates.
private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownItem item = (DropDownItem)cmbType.SelectedItem;
if (!String.IsNullOrWhiteSpace(item.Text))
{
cmbBrand.Enabled = true;
btnAddBrand.Enabled = true;
mainItemInfo.FillBrands(new Dictionary<string, string> { { "Type", item.Text } });
mainItemInfo.SyncBrands(this);
}
}
public void FillBrands(Dictionary<string, string> columnsWhere)
{
// Clear list
Brands.Clear();
// Get data
StorageData storage = new StorageData(File.ReadAllLines(ItemsFilePath));
// Fill Brands
foreach (string type in storage.GetDistinctWhere(columnsWhere, "Brand"))
{
Brands.Add(type, new DropDownItem(type, type));
}
}
public void SyncBrands(IPopupItemInfo form)
{
form.ClearcmbBrand();
var brands = from brand in Brands.Keys
orderby Brands[brand].Text ascending
select brand;
foreach (var brand in brands)
{
form.AddTocmbBrand(Brands[brand]);
}
}
public void AddTocmbBrand(DropDownItem brand)
{
cmbBrand.Items.Add(brand);
}
public void ClearcmbBrand()
{
cmbBrand.Items.Clear();
}
Simply, you can add an item then clear the combobox again:
cmbBrand.Items.Clear();
cmbBrand.Items.Add(DBNull.Value);
cmbBrand.Items.Clear();
You should able to set the datasource of listbox2 to null to clear it, then set it again with the new data.
So, in pseudo-code, something like:
ItemSelectedInListBox1()
{
List futureListbox2Items = LoadOptionsBaseOnSelectedItem(item)
Listbox2.Datasource = null
Listbox2.Datasource = futureListBox2Items
}
That should refresh the list of items displayed in Listbox2 with no white spaces.
I was able to fix the extra space. I changed the Add and Clear methods to:
public void AddTocmbModel(DropDownItem model)
{
cmbModel.Items.Add(model);
cmbModel.DropDownHeight = cmbModel.ItemHeight * (cmbModel.Items.Count + 1);
}
public void ClearcmbModel()
{
cmbModel.Items.Clear();
cmbModel.DropDownHeight = cmbModel.ItemHeight;
}

Set value for same cell in "DevExpress XtraGrid CellValueChanging" event

I have a XtraGrid with one GridView, with a column with checkbox repository item. Now I am handling the CellValueChanging event because I want to only allow the user to check or uncheck based on calculations on other column values on the same row hence I need the e.RowHandle and e.Column of this event and this cannot be done on the EditValueChanging of the repository control.
Now somewhere my calculations say that user cannot check a particular cell to and I throw a message box and try Me.BandedGridView1.SetRowCellValue(e.RowHandle, e.Column, False) but unfortunately this does not set the value to false of that cell.
I need to do it here and here only because of the huge number of calculations based on other column values and I need to set value of the current cell whose event I'm handling right.
Please help.
I'm using DevExpress 9.2 (no chance of upgrading to higher version)
Try this code it's working perfectly !
private void GridView1_CellValueChanged(object sender, CellValueChangedEventArgs e)
{
if (e.Column.Caption != "yourColumnCaption") return;
GridView1.SetFocusedRowCellValue("yourColumnFieldName", 1);
}
You might want to prevent updates by handling ShowingEditor event.
class TestData
{
public TestData(string caption, bool check)
{
Caption = caption;
Check = check;
}
public string Caption { get; set; }
public bool Check { get; set; }
}
Initialize some test data:
BindingList<TestData> gridDataList = new BindingList<TestData>();
gridDataList.Add(new TestData("First row", true));
gridDataList.Add(new TestData("Second row", true));
gridControl.DataSource = gridDataList;
Handle ShowingEditor. Check if user is allowed to change chechbox. If not, cancel the event.
private void gridView1_ShowingEditor(object sender, CancelEventArgs e)
{
GridView view = sender as GridView;
// Decision to allow edit using view.FocusedRowHandle and view.FocusedColumn
if (view.FocusedColumn.FieldName == "Check")
{
// Allow edit of odd rows only
bool allowEdit = view.FocusedRowHandle % 2 == 1;
e.Cancel = !allowEdit;
}
}

Resources