[Q] How To: Randomize a list?? - Java for Android App Development

Dear Friends,
I'm working on an application which has a list of 5 items connected to a database at the back end. I would like to know how can I randomize the order of appearance of these 5 items in these list every time the app start?
Ex
Item 1: A
Item 2: B
Item 3: C
Item 4: D
Item 5: E
Next time the app launches the order of appearance is at random.
Ex
Item 1: C
Item 2: E
Item 3: A
Item 4: D
Item 5: B
So, on and so forth for every instances of app launch.
Also, how can I add transition animation (fade in, fade out, slide in, etc) like most of the modern web-pages have for the app UI. I do not have much programming experience. Just a learner. All help will be appreciated.

Look on the animations tutorial at androidhive.info ( i think it's proper address ).
Answering the first question, I would write method for database that returns list of items and implement randomization in it. Then just create instance of database in onCreate and make just like this : List<Item> randomList = db.returnRandomList();

I think this would be the easiest and most explanatory way that you could understand for the random list
Code:
private ArrayList<String> random(ArrayList<String> list)
{
ArrayList<String> randomList = new ArrayList<String>();
while(list.size() > 0)
{
int randomInt = new Random(System.currentTimeMillis()).nextInt(list.size());
randomList.add(list.get(randomInt));
list.remove(randomInt);
}
return randomList;
}
So with that imagine doing it with "_id"'s before asking the cursor for rows (though you would need all the primary keys to start with to do that on)... hope that gives you enough to start with
as for anims thats part of the view class and much info on d.android.com

To randomize the list use Java's Collections. Shuffle method and pass in ur list.
In order to display animations, after u call startactivity, make a call to overridependingtransition and pass in animations for the entering and exiting activity.
Sent from my XT1022 using XDA Free mobile app

Related

event handling in dynamically created control

Hi all,
I am using eVC++ 4.0, and i've dynamically created a CListView like this:
Code:
lv.Create(0,_T(""),WS_CHILD|WS_VISIBLE,r,this,5);
but I dont know how to handle the events of this control... any ideas??
Mohammad
To get events from a listview (win32) I normally subclass it. I use the subclassing routine to post a message back to the parent when the user is doing something like tapping on it, then the windows routine can check whats selected etc and act on it. Is subclassing possible in mfc ?( I don't use it).
Thank u
but can anybody post some code??
thnx
Ok, I am a bit lazy to look up code at the moment, but here's something:
Yes, subclassing is possible in MFC. You just derive your class from the basic class provided like this:
Code:
class MyListView : pubic CListView
Then you add message handlers in the normal matter.
EDIT: The following passage is incorrect:
But I think subclassing may not be necessary in you case. List box controls send WM_COMMAND messages with notifications of major events like selection change to the parent window. All you have to do is to create a WM_COMMAND handler in your parent class.
Sorry I was thinking of ListBox not list view when I wrote it.
To manually add message handlers you need to put macros like ON_MESAGE or ON_COMMAND in the DECLARE_MESSAGE_MAP section of the class cpp file. All the detaisl are available on MSDN.
Are you saying that a listview will send the same WM_COMMAND as a list box in mfc? Dose this also happen in win32 made listviews. I have always thought it was a bit too tedious to find out when the user taps an item in the listview.
After reading your post levenum I had a quick look and it says that a WM_NOTIFY gets sent to the parent with a LVN_ITEMCHANGED for example. I had not used the LVN_**** because when I looked at them there was none that seem to deal with selections. I would guess that LVN_ITEMACTIVATE or LVN_ODSTATECHANGED would be usefull for this but then a second tap would not be picked up still leaving me wanting subclassing in many situations to get the users tap.
Ok, I have read what u wrote guys and my problem is almost solved, I used the OnNotify() method to handle messages sent from child controls like this:
Code:
BOOL CTest2Dlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
// TODO: Add your specialized code here and/or call the base class
if(wParam ==5 ) //5 is control ID
{
NMHDR *ph =(NMHDR*)lParam;
if(ph->code==NM_CLICK)
MessageBox("Click");
else if(ph->code==HDN_ITEMCLICK)
MessageBox("Item Click");
else
{
CString str;
str.Format("%d",ph->code);
MessageBox(str);
}
}
return CDialog::OnNotify(wParam, lParam, pResult);
}
Now there still a very small prolem: what messages should I handle for 'Selected Item Changed' event ?? I know its easy but I couldnt find it
Regards
mohgdeisat: I am sorry, I made a mistake about WM_COMMAND.
OdeeanRDeathshead is right, I was thinking of a list box not list view.
To make up for this, here is some sample code from a win32 dialog app that uses list view control. I hope it will be of some help:
Code:
//this is from the dialog window function:
case WM_NOTIFY: //handle events:
nmhdr = (LPNMHDR)lParam;
switch (nmhdr->idFrom)
{
case IDC_LEDLIST: //list control ID.
return OnLEDListEvent(hwndDlg, (LPNMLISTVIEW)lParam);
break;
}
break;
BOOL OnLEDListEvent(HWND hwndDlg, LPNMLISTVIEW nmlv)
{
/////
// Handles list view control notification messages
switch (nmlv->hdr.code)
{
case LVN_ITEMCHANGED:
return OnLEDListItemChanged(hwndDlg, nmlv);
break;
}
return 0;
}
BOOL OnLEDListItemChanged(HWND hwndDlg, LPNMLISTVIEW nmlv)
{
if (ListView_GetSelectionMark(nmlv->hdr.hwndFrom) != nmlv->iItem) return 0;
/* do what you need here */
return 0;
}
Don't mind the fact that I used 3 different functions. This is part of a bigger program and I am trying to keep things well organized without resorting to classes.
As I understand it, LVN_ITEMCHANGE is received for different reasons so I try to handle only the one coming from selected item. LVN_ITEMACTIVATE is only sent when you double click the item so if you just want to catch selection change you need to use LVN_ITEMCHANGE.
Once again, sorry for confusing you before.
thanx pals, good job!!!
I think my problem is now solved with ur help :wink:
Mohammad
Ok guys, I said that my problem was solved, yet, another problem arises...
When the list view is in the report mode, how can I determine when a header button is clicked, and determine which one was clicked???????
thanx in advance
To identify a header column click, you need to handle the WM_NOTIFY/HDN_ITEMCLICK message. Normally this message will be received by the header's parent control (i.e. the listview) -- some frameworks may redirect the message to the header control itself. I haven't worked with MFC in 10 years so I can't really if it reflects notification messages back to the control.
If you're trying to implement column sort, do yourself a favor and check out the Windows Template Library (WTL) at sourceforge.net. It's a set of C++ template classes that provide a thin yet useful wrapper around the standard Windows UI components. One of the classes is a sortable listview control. I've been using WTL with big Windows for more than 5 years -- you couldn't pay me to go back to MFC.
hi,
I have seen the WTL library and it seems very useful and time-saver, but I have a couple of questions about it:
1. can WTL 8.0 be installed with VC++ 6.0, specifically the appwizard stuff??how?? I see only javascript files of vc7.0 and 7.1 and 8.0!!
2. is there a good documentation about those classes??
Mohammad
I don't know about WTL 8; I'm still using WTL 7.5 with VS .Net 2003 for all my Win32 development. My guess is that it wouldn't work too well, as WTL is based on ATL, which has substantially changed between VC 6 and 7.
Good references for WTL include www.codeproject.com/wtl, and the WTL group over at Yahoo groups (reflected at www.gmane.org).

TabHost recreating instances

I'm developing a simple android application using the TabActivity.
This Activity adds 3 tabs, each one with its own activity.
The problem is: when I tap each tab, the system calls onDestroy of the current activity. So, when I tap the first activity again, all the state of private fields are gone, its like it has created a new instance every time I change tabs.
I was searching around about this, and looking on the samples, but I guess that the default behavior is to keep the activity instances paused when the tab is not current.
Anyone have an idea of what I'm missing?
Thanks.
That is by design. You need to override onSaveInstanceState() and onRestoreInstanceState() in your activity class. You can store/retrieve state data there, or retrieve it int the Bundle passed into OnCreate(). See:
http://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle)

A paragraph about Intent concept

Hi
In the book that I am reading there is a paragraph about teaching the Intent concept :
"" An intent generically defines an intention to do some work. Intents encapsulate several
concepts, so the best approach to understanding them is to see examples of their use.
You can use intents to perform the following tasks:
Broadcast a message.
Start a service.
Launch an activity.
Display a web page or a list of contacts.
Dial a phone number or answer a phone call.
Intents are not always initiated by your application—they’re also used by the system to
notify your application of specific events (such as the arrival of a text message).
Intents can be explicit or implicit. If you simply say that you want to display a URL, the
system decides what component will fulfill the intention. You can also provide specific
information about what should handle the intention. Intents loosely couple the action
and action handler. ""
I completely understand the paragraph except the final portion that I underlined it .
What does that mean ???
Please explain it for me .
What is action and action handler ???
Thanks for your help
The action is what you want to do (e.g.: show a website) and the action handler is the app that can handle the action (e.g: the browser app).
So a intent combines that two things: the information "show a website" and the app that can do what you want (the browser)
@EmptinessFiller is right .
Suppose you want to open an image generated by your app. Then you would have the following options:-
Option 1
Implement a custom Activity and send an intent to start it so the image can be processed.
Option 2
Launch an intent to any other app like the gallery app to handle the intent.(Display your image)
Give a thanks if you think this post helped you!
Sent from my Nexus 4 using XDA Premium 4 mobile app
The 'loosely' in my opinion refers to the possibility to either exactly define who you want to do your action (an explicit intent) but you can as well just leave open who does that (an implicit intent), then you will get that well known 'complete action using...' dialogue
So action and its handler can but dont have to be coupled inside the intent
---------------------------------
Phone : My new Nexus 4!
OS:
Pure KitKat 4.4.2 stock, no root, no mods (but only for the first time ;D)
---------------------------------
Gesendet von Tapatalk

[Tutorial] Learn to create a Countdown Timer on Android

Hello,
I create that thread to present you a tutorial showing you how to create a a Countdown Timer on Android. The tutorial is also available on my website here : http://www.ssaurel.com/blog/learn-to-create-a-countdown-timer-on-android/ .
Sometimes, when you make Android applications, you need to create a countdown. It can be useful in a board game for example if the player have a defined time to play.
A lot of solutions exist to create a countdown in Java. Some are more complex than other.
On Android, the SDK offers to developers an easy way to create a countdown timer : a dedicated class named CountDownTimer.
1/ Create the CountDown Timer
Here, we consider a countdown of 60 seconds. So, we need to use the following code to create the CountDownTimer instance :
Code:
CountDownTimer countDownTimer = new CountDownTimer(60 * 1000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("Seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("Done !");
}
};
The second parameter lets you to define at which interval you must be notified during the countdown. You can update your User Interface in that method.
2/ Manage your CountDown Timer
Once you have created your CountDownTimer object, you have to manage it. First, you can start it :
Code:
countDownTimer.start();
If you want to cancel your countdown timer before its end, you have just to call its cancel() method :
Code:
countDownTimer.cancel();
3/ Conclusion
Powerful and easy to use, the CountDownTimer class is the ideal solution to create countdown timer on Android.
4/ Extra
You can also enjoy this tutorial on Youtube :
Don't hesitate to give me your feedbacks and ideas for new tutorials.
Thanks.
Sylvain
Some feedbacks ?

start building app.. and immediately need your help

Hello.
I'm very new to app developing but for starting learning I'd like to modify an app I have, doing two things:
1) in this app I have an edit text (with the cursor blinking) and a custom keyboard (not the system one, but one just of that app) for writing on it (something like a lockscreen). The problem is that whenever I press the keyboard buttons, despide of seeing my touch on them, nothing appear in the edit text.
Here's the layout.xml
Code:
<com.android.internal.widget.PasswordEntryKeyboardView android:id="@id/keyboard" android:background="#00000000" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:keyBackground="@drawable/btn_keyboard_key_fulltrans" />
Is this right?
Where can I find why it's not writing? In the PasswordEntryKeyboardView.smali?
2) I'd like also to have the status bar visible but not expandable. I've found I should do it putting a overlay over status bar and consumed all input events. So I'd need a custom class which extends any layout and consumes touch event and then to consume touch event override the onInterceptTouchEvent method of the view group and return true.
This should be the code for that custom class (customViewGroup):
Code:
WindowManager manager = ((WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
// this is to enable the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
localLayoutParams.height = (int) (50 * getResources()
.getDisplayMetrics().scaledDensity);
localLayoutParams.format = PixelFormat.TRANSPARENT;
customViewGroup view = new customViewGroup(this);
manager.addView(view, localLayoutParams);
So, how to do it...
Do I have to add a new .smali in the widget folder with that code or can I add that code in a file there?
And how to override the onInterceptTouchEvent method of the view group and return true?
And how to remove it when I leave that app?
I hope someone helps me learning.
Thanks.

Categories

Resources