how to tackle setContentView with condition... - Java for Android App Development

I'm trying to build a app that frst checks for valid emal ids and accodingly executes the code..
I have two layouts where
one is the actuall app..
two is a exit page..
i get an eror and i understand y... but how do i solve it??
So in a simple way this is the code
Code:
get user details
if(condition)
setContentView(xyz)
code_xyz
else
seContentView(abc)
code_abc..
code_abc..
what happns after the setConentView(abc) is the code fr the actual app...
whenn rogue user is detected, i set content view to xyz, it continues to execute rmaining code meant for abc... how do i stop this??
code_xyz contans code for one exit button.. thats it..
i want code to stop at that point... How do i this??

nvyaniv said:
I'm trying to build a app that frst checks for valid emal ids and accodingly executes the code..
I have two layouts where
one is the actuall app..
two is a exit page..
i get an eror and i understand y... but how do i solve it??
So in a simple way this is the code
Code:
get user details
if(condition)
setContentView(xyz)
code_xyz
else
seContentView(abc)
code_abc..
code_abc..
what happns after the setConentView(abc) is the code fr the actual app...
whenn rogue user is detected, i set content view to xyz, it continues to execute rmaining code meant for abc... how do i stop this??
code_xyz contans code for one exit button.. thats it..
i want code to stop at that point... How do i this??
Click to expand...
Click to collapse
It should work like this, don't forget the curly brackets around the statements between if() and else...

As said SimplicityApks, brackets are very important, you must use them if you have more than one line of code after "if" and "else", otherwise it will not work.
the correct structure in your case is :
if (condition) {
setContentView(view1);
do somthing X;
} else {
setContentView(view2);
do somthing Y;
}
You can do it this way too:
if (condition) {
setContentView(view1);
do somthing X;
return;
}
setContentView(view2);
do somthing Y;

ok my bad.. i guess my bad for missing out on few more details..
once we out of the main function... there are a couple more outsie of it which get executed... that is the problem.. this are mostly like your onclick listner and stuff...
since these buttons etc are part of main, it pops an error when the condition goes to the exit page...
Hope i waas able to explain it properly...

nvyaniv said:
ok my bad.. i guess my bad for missing out on few more details..
once we out of the main function... there are a couple more outsie of it which get executed... that is the problem.. this are mostly like your onclick listner and stuff...
since these buttons etc are part of main, it pops an error when the condition goes to the exit page...
Hope i waas able to explain it properly...
Click to expand...
Click to collapse
Now I get it! You can stop a method by calling return; or return val; if the method shall return anything. Note that if you are executing code for both layouts in more than one method you should check the condition every time or use a boolean instance variable with an if(condition) {code(); }else {code2();}

Related

Turn off the phone part of the XDA

I am writing an application for the XDA using eVC++ 3.0 with the Pocket PC 2002 SDK.
What I need to do is turn on/off the phone part of the XDA programatically without displaying the progress bubbles that normally appear.
I have thought about sending keydown and keyup events if I can find the correct key codes but the progress bubbles still appear.
Any thoughts?
Code:
//-------------------------------------------------------------
HINSTANCE hCCoreUtlDll;
typedef void (__cdecl *PSRSFN)(int state);
PSRSFN SetRadioState;
BOOL LoadCCoreUtl()
{
hCCoreUtlDll= LoadLibrary(_T("ccoreutl.dll"));
if (hCCoreUtlDll==INVALID_HANDLE_VALUE)
return FALSE;
SetRadioState= (PSRSFN)GetProcAddress(hCCoreUtlDll, (TCHAR*)0x10);
return (SetRadioState != NULL);
}
void TurnRadioOn()
{
SetRadioState(2);
}
void TurnRadioOff()
{
SetRadioState(1);
}
From reverse engineering rsupgrade.exe.
Code not tested, but I think this is how to do it.
are there other functions for getting the current radio state? presumably so. can someone publish a complete list of funtion ordinals for ccoreutl.dll? sounds like it might have some useful stuff in it.
on a related note - does anyone know where the functionality for entering the SIM pin code lives? Ideally I want to be able to programmatically turn the radio on and input the PIN code without being pestered for it.
thanks,
nick.
Ah those were the days...
Just happen to make it to the last page of this forum. This thread is an example of what was on our minds then.....5 years ago.
WB

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).

[Q] Dialogs and Screen rotation

I saw a lot of ppl having this problem and it's something SO common to do, and the solutions I saw were ugly to the point that I bet that Google's solution for it isn't any of those.
Simple scenario, I have a few dialogues in my application being setup inside:
Code:
protected Dialog onCreateDialog(int id) {
switch(id){
case DIALOG_LOAD: return new ProgressDialog... etc.. etc..
}
}
and called with
Code:
showDialog(DIALOG_LOAD);
and dismissed with
Code:
dismissDialog(DIALOG_LOAD);
all inside the activity class.
This approach to dialogues I believe is the 'cleanest' way of doing it and just let the Activity manages the dialogues.
it works fines IF I don't rotate the device, I though because the activity was managing it, it would be all inclusive, but no, life is never that simple. If I rotate the phone when the dialogue is up, the rotation occurs, the dialogue is still visible but the moment the code try to dismiss it it gives a nice:
05-26 20:07:02.951: ERROR/AndroidRuntime(10041): java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog
so...
do I have to care about dismissing and re-creating the dialogue onPause and onResume or is there anything I can do to make the activity take care of it by itself?

AsyncTask not working

Here is my code:http://pastebin.com/Xq7wkBLk
Whenever I press the load button the textview never changes.. its as if the buttons are not working.. I hope anyone can help me.
First, use a bunch of Log.d(string, sting) to check if your methods are called, makes life much easier.
Second, just a wild try.. Try in your postExeculte: InternalData.this.dataResult.setText(result);
prototype-U said:
Here is my code:http://pastebin.com/Xq7wkBLk
Whenever I press the load button the textview never changes.. its as if the buttons are not working.. I hope anyone can help me.
Click to expand...
Click to collapse
I am not positive but in your doInBackground you return null. So in the postExecute the result is null and you are setting the text to the result. That seems to me what your problem might be. try return a string that says failed to see if it is going to that return statement.
You haven't declared what the String 'result' is, so it looks like you have trying to setText to nothing.
It could also be what Acela said, instead of returning a string in doInBackground, you have it set to return null.
LOL guys the error is on line 113 and 117.. spelling mistake..
and now its workng like a charm..
forgot to get this thread closed
Thread Closed as per OP....

Guys!! Please help me!!

Guys. Help me. I'm learning to ''Respond to the Send Button'' but I don't know what to do. I follow this step but I still can't understand.
http://developer.android.com/training/basics/firstapp/starting-activity.html
If you guys can help me, I'm really grateful.
Merivex95 said:
Guys. Help me. I'm learning to ''Respond to the Send Button'' but I don't know what to do. I follow this step but I still can't understand.
http://developer.android.com/trainin...-activity.html
If you guys can help me, I'm really grateful.
Click to expand...
Click to collapse
That link didn't work, but if you Google 'android java button onclick listener' that will get you started with plenty of helpful links.
When the button is clicked, you then need to check the content of the Edit Text field - Something like this:
Code:
public void onClick(final View v) {
final String commandText = inputText.getText().toString();
if (commandText != null && commandText.length() > 0) {
Where inputText is the Edit Text field you've assigned in your OnCreate method.
Getting started involves a lot of head scratching, but don't give up! There's so much Open Source code out there, that the penny will drop soon.
brandall said:
That link didn't work, but if you Google 'android java button onclick listener' that will get you started with plenty of helpful links.
When the button is clicked, you then need to check the content of the Edit Text field - Something like this:
Code:
public void onClick(final View v) {
final String commandText = inputText.getText().toString();
if (commandText != null && commandText.length() > 0) {
Where inputText is the Edit Text field you've assigned in your OnCreate method.
Getting started involves a lot of head scratching, but don't give up! There's so much Open Source code out there, that the penny will drop soon.
Click to expand...
Click to collapse
OMG !! this is the link : http://developer.android.com/training/basics/firstapp/starting-activity.html
I don't know how to compile the code properly. hummmm ... maybe you can help me by correcting my code ?
Paste code (variables, functions, etc) inside your MainActivity class and don't write 3 dots, it's just a replacement for skipped parts
Mikanoshi said:
Paste code (variables, functions, etc) inside your MainActivity class and don't write 3 dots, it's just a replacement for skipped parts
Click to expand...
Click to collapse
Ohhh yeahhh :cyclops: .. how can I be so stupid
btw ,, where should I put this code ?
code:
Code:
@override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
Code:
Merivex95 said:
btw ,, where should I put this code ?
Click to expand...
Click to collapse
Replace existing onCreate function with it.
Mikanoshi said:
Replace existing onCreate function with it.
Click to expand...
Click to collapse
But I get an error after doing that ..
Merivex95 said:
But I get an error after doing that ..
Click to expand...
Click to collapse
You need to import TextView and Intent first. Just hover over the names and select import from the menu you will see.
Seriously, my advice would be to learn and understand Java first. It doesn't make sense to learn Android if you don't know Java (and it won't work).
nikwen said:
You need to import TextView and Intent first. Just hover over the names and select import from the menu you will see.
Seriously, my advice would be to learn and understand Java first. It doesn't make sense to learn Android if you don't know Java (and it won't work).
Click to expand...
Click to collapse
Thanks for the advice
nikwen said:
It doesn't make sense to learn Android if you don't know Java (and it won't work).
Click to expand...
Click to collapse
Unless you know any other similar languages. Java was the easiest lang to learn for me, after years of writing on Object Pascal/Object C/C++/JavaScript/PHP. The most troublesome part of coding for Android is creating an UI, especially cross-version compatible :laugh:

Categories

Resources