why AlertDialog doen't wait for button click ??

Put your problem here if it does not fit any of the other categories.

why AlertDialog doen't wait for button click ??

Postby ranjit0046 » Mon Jan 18, 2010 10:23 am

Hi friends,

i wrote java code that displays alert dialog, but the control does n't wait for button click . it directly execute remaining code as it is. i want to perform some action when ever button clicks but it does n't waits !! please tell me any way to get rid of this ..Its very urgent.... My code is as bellow

public static int alertbox(String title, String mymessage,String softkeyvalue,Context context)
{


Log.i("this is the context ","context in Query text "+context);
FileInputStream fis;

String lineString;
String softkeyfilepath=context.getFilesDir()+"/softkeys.txt";
Log.i("file path for softkeys = ",softkeyfilepath);
File rFile = new File(softkeyfilepath);

HashMap<String,String> softkey = new HashMap<String,String>();
try
{

if (rFile.exists() && rFile.canRead())
{
fis = new FileInputStream(rFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((lineString=br.readLine())!= null)
{
String[] str=lineString.split("=");
softkey.put(str[1].trim(), str[0].trim());

}//while


}//if


String value=softkey.get(softkeyvalue);
Log.i("button names","=="+value);

if(value.contains("_"))
{
String softkeyname[]=value.split("_");


new AlertDialog.Builder(context)
.setMessage(mymessage)
.setTitle(title)
.setCancelable(true)
.setPositiveButton(softkeyname[0],new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
Log.i("this is first button","first button");
m_query = 0;

}
}
)
.setNegativeButton(softkeyname[1],new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
Log.i("this is first button","second button");
m_query = 1;
}
}
)

.show();

}
else
{
new AlertDialog.Builder(context)
.setMessage(mymessage)
.setTitle(title)
.setCancelable(true)
.setPositiveButton(value,new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
Log.i("this is first button","single button");
m_query = 0;
}
}
)
.show();

}


}//try
catch(Exception e)
{
e.printStackTrace();
}
return m_query;
}




Please help me .........




Ranjit....................
ranjit0046
Developer
Developer
 
Posts: 47
Joined: Mon Dec 14, 2009 12:46 pm

Top

Postby jwei512 » Mon Jan 18, 2010 11:11 am

So yea it seems like you need some cleaning up to do

First off I would use the activity's onCreateDialog method to help you organize your alert dialogs, etc (see http://developer.android.com/guide/topi ... alogs.html on how to do this properly) and then i would fix up your method by doing something like:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public static void alertbox(String title, String mymessage,String softkeyvalue,Context context)
  3.  
  4. {
  5.  
  6.  
  7.  
  8.  
  9.  
  10. Log.i("this is the context ","context in Query text "+context);
  11.  
  12. FileInputStream fis;
  13.  
  14.  
  15.  
  16. String lineString;
  17.  
  18. String softkeyfilepath=context.getFilesDir()+"/softkeys.txt";
  19.  
  20. Log.i("file path for softkeys = ",softkeyfilepath);
  21.  
  22. File rFile = new File(softkeyfilepath);
  23.  
  24.  
  25.  
  26. HashMap<String,String> softkey = new HashMap<String,String>();
  27.  
  28. try
  29.  
  30. {
  31.  
  32.  
  33.  
  34. if (rFile.exists() && rFile.canRead())
  35.  
  36. {
  37.  
  38. fis = new FileInputStream(rFile);
  39.  
  40. DataInputStream in = new DataInputStream(fis);
  41.  
  42. BufferedReader br = new BufferedReader(new InputStreamReader(in));
  43.  
  44. while ((lineString=br.readLine())!= null)
  45.  
  46. {
  47.  
  48. String[] str=lineString.split("=");
  49.  
  50. softkey.put(str[1].trim(), str[0].trim());
  51.  
  52.  
  53.  
  54. }//while
  55.  
  56.  
  57.  
  58.  
  59.  
  60. }//if
  61.  
  62.  
  63.  
  64.  
  65.  
  66. String value=softkey.get(softkeyvalue);
  67.  
  68. Log.i("button names","=="+value);
  69.  
  70.  
  71.  
  72. if(value.contains("_"))
  73.  
  74. {
  75.  
  76. String softkeyname[]=value.split("_");
  77.  
  78.  
  79.  
  80. showDialog(DIALOG_NAME_ONE);
  81.  
  82.  
  83.  
  84. } else {
  85.  
  86.  
  87.  
  88. showDialog(DIALOG_NAME_TWO);
  89.  
  90.  
  91.  
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98. }//try
  99.  
  100. catch(Exception e)
  101.  
  102. {
  103.  
  104. e.printStackTrace();
  105.  
  106. }
  107.  
  108. return;
  109.  
  110. }
  111.  
  112.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Then, make m_query a global variable, and in the construction of your alert boxes just set m_query inside the onClickListener

This should control the flow of your dialogs and stuff and help you avoid the problem of m_query being returned before it is actually set
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests