Blackberry ListField: how to add Listener for list row click - user-interface

Suppose i had a Screen class that has 10 ListField:
Vector v_prj_title,v_prj_mgr
// v_prj_title contains name of projects
// v_prj_mgr contains name of the project_manager of v_prj_title sequentially.
//Vector send_vector
//ListField myList
//ListCallBack callback
//It is clear from the code that in myList, I m inserting a vector send_vector ie callback.insert(send_vector,i), which contains 2 strings collected one from v_prj_title and other from v_prj_mgr.
for(int i=0;i<10;i++)
{
myList.insert(i);
t1 = v_prj_title.elementAt(i).toString();
send_vector = new Vector(2);
send_vector.addElement(t1);
t2 = v_prj_mgr.elementAt(i).toString();
send_vector.addElement(t2);
callback.insert(send_vector,i);
}
Now I'm getting confused how to add eventListener to particular ListField, e.g. suppose if I click the 3rd ListField,(suppose this is the displayed data below) a bitmap picture should be displayed in the 3rd ListField and the name of the project (Project_Social_Meeting) and project_manager (Tom Clerk) should be inserted into database (SQlLite)
1. a. Project_Chat_Master( project name)
b. Vyom Ryan (project manager)
2. a. Project_Online_Gaming
b. Vivek Roy
3. a. Project_Social_Meeting
b. Tom Clerk
.
.
etc.....

create a CustomField/Manager depending on your requirement.(which may contain images/strings/...)
then add them to the callback method
Ex:
step:1
//creating a custom field
class MYListFieldItem extends Field
{
//#override
paint(graphics g)
{
g.drawbitmap(bitmap,0,0);
g.drawtext(string,bitmap.getwidth()+5<padding>,Math.min(bitmap.getHeight(),getFont().getHeight()));
//#override
layout(....)
{
setExtent(Math.min(width,bitmap.getwidth()+padding+getfont.getadvance(stringtext)),
Math.min(height,Math.min(bitmap.getHeight,getFont().getHeight())));
}
}
step-2:
//create list items
MYListFieldItem [] fields[] = new MYListFieldItem [<numOfListItems>];
for(int i=0;i<fields.size;i++)
{
_callback = new MyListFieldCallBack();
_callback.insert(fields[i],i);
}
step-3:
//set listeners
mylistFielditem[i].setchangeListener(new fieldchangeListener(){
fieldChanged(field)
{
//do your action here.
});
//TIP: if the fields are just strings,
//#override
navigationclick()
{
if(status == keypadlistener.status_fourway)
{
MYListFieldItem fld = (ListField)getLiefFieldwithFocus();
//do your coding
}
}

Related

xamarin android: showing ads in recycleview hide some articles in rss entries

I'm trying to show ads in recycleview and i succeeded to do it using the code below .. the problem is that in every "MspaceBetweenAds" position the ad show up but the article at this replaced with the ad
i tried to fix it by modifying ItemCount() by Mposts.Count + (Mposts.count% MspaceBetweenAds) but i'm getting "IndexOutOfBounds " error
any help please .. this is my code
public class AdsView : ListViewHolder
{
public AdView mAdView { get; private set; }
public AdsView(View view) : base(view)
{
mAdView = view.FindViewById<AdView>(Resource.Id.AdsCard);
}
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
RecyclerView.ViewHolder vh = null;
switch (viewType)
{
case 1:
View vBig = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.BigCard, parent, false);
vh = new MyView(vBig);
break;
case 2:
View vAds = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.AdsCard, parent, false);
vh = new AdsView(vAds);
break;
}
return vh;
}
public override void OnBindListViewHolder(ListViewHolder holder, int position)
{
var MyHolder = holder as MyView;
switch (holder.ItemViewType)
{
case 1:
// code to show posts articles here
break;
case 2:
var AdHolder = holder as AdsView;
fnc.AddBannerAd(AdHolder.mAdView);
break;
}
}
public override int GetItemViewType(int position)
{
if (position > 0 && position % mSpaceBetweenAds == 0) { return 2; }
else { return 1; }
}
and this is a demo app https://drive.google.com/open?id=1Tk3G8dw9nqIffxmEFNGqIgXNzCJJPxD_
As the demo you posted contains 3rd party package, I cannot run it directly and modify the demo for you. The flowing is my solution to your problem:
Error causes:
The way you do (modifying ItemCount() by Mposts.Count + (Mposts.count% MspaceBetweenAds) cannot change the real length of Mposts, as a result, it results in the
"IndexOutOfBounds " error.
Solution
If you want to insert ads into your recylerview, you need not only modify your adapter to show both item and ads, but also need to modify your layout resource file, that is, you need to insert the ads data into the data list for of your recylerview. Or you can simply add a duplicated item to the list every [MspaceBetweenAds]items.

CharmListView Infinite Scroll

I need basically an event that triggers at each 200 records loaded, so more data can be loaded until the end of data.
I tried to extend CharmListCell and using the method updateItem like this:
#Override
public void updateItem(Model item, boolean empty) {
super.updateItem(item, empty);
currentItem = item;
if (!empty && item != null) {
update();
setGraphic(slidingTile);
} else {
setGraphic(null);
}
System.out.println(getIndex());
}
But the System.out.println(getIndex()); method returns -1;
I would like to call my backend method when the scroll down gets the end of last fetched block and so on, until get the end of data like the "infinite scroll" technique.
Thanks!
The CharmListCell doesn't expose the index of the underlying listView, but even if it did, that wouldn't be of much help to find out if you are scrolling over the end of the current list or not.
I'd suggest a different approach, which is also valid for a regular ListView, with the advantage of having the CharmListView features (mainly headers and the refresh indicator).
This short sample, created with a single view project using the Gluon IDE plugin and Charm 5.0.0, shows how to create a CharmListView control, and fill it with 30 items at a time. I haven't provided a factory cell, nor the headers, and for the sake of simplicity I'm just adding consecutive integers.
With a lookup, and after the view is shown (so the listView is added to the scene) we find the vertical ScrollBar of the listView, and then we add a listener to track its position. When it gets closer to 1, we simulate the load of another batch of items, with a pause transition that represents a heavy task.
Note the use of the refresh indicator. When new data is added, we scroll back to the first of the new items, so we can keep scrolling again.
public class BasicView extends View {
private final ObservableList<Integer> data;
private CharmListView<Integer, Integer> listView;
private final int batchSize = 30;
private PauseTransition pause;
public BasicView() {
data = FXCollections.observableArrayList();
listView = new CharmListView<>(data);
setOnShown(e -> {
ScrollBar scrollBar = null;
for (Node bar : listView.lookupAll(".scroll-bar")) {
if (bar instanceof ScrollBar && ((ScrollBar) bar).getOrientation().equals(Orientation.VERTICAL)) {
scrollBar = (ScrollBar) bar;
break;
}
}
if (scrollBar != null) {
scrollBar.valueProperty().addListener((obs, ov, nv) -> {
if (nv.doubleValue() > 0.95) {
addBatch();
}
});
addBatch();
}
});
setCenter(new VBox(listView));
}
private void addBatch() {
listView.setRefreshIndicatorVisible(true);
if (pause == null) {
pause = new PauseTransition(Duration.seconds(1));
pause.setOnFinished(f -> {
int size = data.size();
List<Integer> list = new ArrayList<>();
for (int i = size; i < size + batchSize; i++) {
list.add(i);
}
data.addAll(list);
listView.scrollTo(list.get(0));
listView.setRefreshIndicatorVisible(false);
});
} else {
pause.stop();
}
pause.playFromStart();
}
}
Note also that you could benefit from the setOnPullToRefresh() method, at any time. For instance, if you add this:
listView.setOnPullToRefresh(e -> addBatch());
whenever you go to the top of the list and drag it down (on a mobile device), it will make another call to load a new batch of items. Obviously, this is the opposite behavior as the "infinite scrolling", but it is possible as well with the CharmListView control.

JavaFX Selecting a TREE ITEM behaves does not work in the first walk behaves correctly after that

I have a tree of family members. I am attempting to provide a SEARCH facility which will allow me to select all the tree items where person name contains the SEARCH name. See this screen shot, though the first item matches RAJA it is not selected.
Now when I click on the LOCATE button again, the selections are correct as you can see.
This is the code for LOCATE BUTTON CLICKED.
#FXML
void onLocateClicked(MouseEvent event) {
childrenTreeView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
childrenTreeView.getSelectionModel().clearSelection();
String tmpLocateString = txtLocate.getText().toLowerCase();
TreeItem<APerson> item = childrenTreeView.getRoot();
item.setExpanded(true);
System.out.println("Locate ->"+tmpLocateString+" Root value = "+item.getValue().getPersonName());
if (item == null) {
// Don't do anything
return;
}
if (item.getValue().getPersonName().toLowerCase().contains(tmpLocateString)){
System.out.println("Item ->"+item+" MATCHES (LocateClicked)");
childrenTreeView.getSelectionModel().select(item);
}
doSearch(item, tmpLocateString);
}
private void doSearch(TreeItem<APerson> item, String tmpLocateString) {
System.out.println("Current Parent :" + item.getValue());
if (item != childrenTreeView.getRoot()) {
if (item.getValue().getPersonName().toLowerCase().contains(tmpLocateString)){
System.out.println("Item ->"+item+" MATCHES (in doSearch)");
childrenTreeView.getSelectionModel().select(item);
}
}
for(TreeItem<APerson> child: item.getChildren()){
String personName = child.getValue().getPersonName();
if (personName.toLowerCase().contains(tmpLocateString)) {
childrenTreeView.getSelectionModel().select(child);
}
if(child.getChildren().isEmpty()){
System.out.println(" No Children for this node : "+personName);
} else {
doSearch(child, tmpLocateString);
}
}
}
After the first error, the selections work correctly for all other searches..... Can anyone guess what is wrong? "doSearch" function is recursive to walk thru entire tree.
Thanks for your help, in advance.
Hornigold
This is the change I did to onLocateClicked but it did not work.
void onLocateClicked(MouseEvent event) {
TreeItem<APerson> item = childrenTreeView.getRoot();
if (item == null) {
// Don't do anything
return;
}
String tmpLocateString = txtLocate.getText().toLowerCase();
childrenTreeView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
childrenTreeView.getSelectionModel().clearSelection();
item.setExpanded(true);
System.out.println("Locate ->"+tmpLocateString+" Root value = "+item.getValue().getPersonName());
if (item.getValue().getPersonName().toLowerCase().contains(tmpLocateString)){
System.out.println("Item ->"+item+" MATCHES (LocateClicked)");
childrenTreeView.getSelectionModel().select(item);
}
doSearch(item, tmpLocateString);
}

Invalidate() and child_wnd_name.InvalidateRect (&rect) cause different results

I am writing a dialog based application which has one static control, one button and two edit controls. The static's type is class MyStatic : public CStatic which has an OnPaint() defined as follows:
void MyStatic::OnPaint()
{
CPaintDC dc(this);
int Row = pBGDlg->iRow; //pBGDlg is a pointer to the dialog
int Column = pBGDlg->iColumn; //iRow and iColumn are variables of the two edit controls
int RowHei = pBGDlg->iRowHei;
int ColumnWid = pBGDlg->iColumnWid;
if (bBuPressed)
{
for (int i = 0; i <= Row; ++i)
{
dc.MoveTo (0, i * RowHei);
dc.LineTo (Column * ColumnWid, i * RowHei);
}
}
Whenever the user input two integers and press the button, the application will draw corresponding lines on the static
void CProjDlg::OnClickedIdpreview()
{
UpdateData ();
mystatic.GetClientRect (&rtStatic); //mystatic is the name of the static
iRowHei = (rtStatic.Height () - 1) / iRow;
iColumnWid = (rtStatic.Width () - 1) / iColumn;
mystatic.bBuPressed = true;
Invalidate ();
UpdateWindow ();
}
For Example:Input 4 to the 1st edit control and then input 1 to the 1st edit control. However, if I substitute mystatic.InvalidateRect(&rtStatic); and mystatic.UpdateWindow(); for Invalidate(); and UpdateWindow(); respectively, the application fails to erase previous results. For example: Input 4 to the 1st edit control and then input 5 to the 1st edit control. I can not figure out why the second method fails. Could anyone explain for me? Thank you very much.

Mouse Click Events on JTable

I want to get the index of selected row when user double clicks on a row.
Here is my code:
tab.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
int selectedRow = tab.getSelectedRow();
try {
String file = rows[selectedRow][2];
String path = "C:\\Users\\raj kumar\\Gallery\\" + file;
JLabel fileLable = new JLabel();
fileLable.setBounds(500, 600, 300, 300);
fileLable.setIcon(new ImageIcon(path));
pan.add(fileLable);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
});
But the tab.getSelectedRow() returns -1 even though I double clicked the row in the table.
You want to know on which row your mouse points, but you ask which row is selected. So the simple solution is, instead of
int selectedRow = tab.getSelectedRow();
you can use
int row = tab.rowAtPoint(e.getPoint());
to get the wanted row. The Event e has every necessary information you need. The e.getPoint() returns the exact Point your cursor is currently located. And the rowAtPoint() should be self explaining.
This also makes sure that you only work with one row at a time, if this is important to you. I don't know how getSelectedRow() works if multiple rows are selected.

Resources