Android dialog not showing

Go To StackoverFlow.com

2

Who would of thought getting a dialog box to show would be so difficult. In C# it is so easy. I've looked all over line for help and every tip seems to cause me trouble than good. Can someone please look at the below code and tell me where I might have gone wrong?

What I want is very simple. User clicks button. Then list appears. Then user clicks on one of the items in list, and dialogue appears. The code in onItemClick in the listview event is from someone else. It apparently works for that person. I keep clicking and seeing nothing. I'm open to suggestions on how to fix it, or new code all together. Sorry, the log has no error messages.

Please be specific about where I put the code as I'm an Android noob. And thank you in advance!

public class SetPrediction extends Activity
{
    Button btnInsrt, btnFTeam, btnSTeam;
    ArrayList<NameValuePair> nameValuePairs;
    TextView txtGameTeams;
    Bundle recdData;
    String game;

    JSONArray jArray;
    String result;
    InputStream is;
    StringBuilder sb;

    ArrayList<String> fNames;
    ListView listView;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.set_prediction);

        nameValuePairs = new ArrayList<NameValuePair>();
        txtGameTeams = (TextView) findViewById(R.id.txtGameTeams);

        recdData = getIntent().getExtras(); 
        game = recdData.getString("xxxx.xxxx.xxx");
        txtGameTeams.setText("xxxxxxx: " + game + " game.");

        btnInsrt = (Button) findViewById(R.id.btnInsert);
        btnFTeam = (Button) findViewById(R.id.btnFirstTeam);
        btnSTeam = (Button) findViewById(R.id.btnSecondTeam);

        btnFTeam.setText(removeSpaces(game.split("vs")[0]));
        btnSTeam.setText(removeSpaces(game.split("vs")[1]));

        btnSTeam.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
                String result = "";
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("players", removeSpaces(game.split("vs")[1])));

                fNames = new ArrayList<String>();
                listView = (ListView) findViewById(R.id.lstPlayerForPrediction);

                //http post
                try
                {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://xxxxx/getTeamPlayers.php");
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost); 
                    HttpEntity entity = response.getEntity();
                    InputStream is = entity.getContent();

                    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) 
                    {
                        sb.append(line + "\n");
                    }
                    is.close();

                    result=sb.toString();
                }
                catch(Exception e)
                {
                    Log.e("log_tag", "Error in http connection " + e.toString());
                }

                //parse json data
                try
                {
                    JSONArray jArray = new JSONArray(result);
                    for(int i=0;i<jArray.length();i++)
                        fNames.add(jArray.getJSONObject(i).getString("First_Name"));  

                }
                catch(JSONException e)
                {
                    Log.e("log_tag", "Error parsing data " + e.toString());
                }

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(SetPrediction.this, android.R.layout.simple_list_item_1, fNames);
                listView.setAdapter(adapter);

                listView.setOnItemClickListener(new OnItemClickListener()
                {
                    public void onItemClick(AdapterView<?> arg0, View predictView, int item, long arg3) 
                    {
                        final CharSequence[] items = {"Online", "Away", "Do not distrub","Invisible","Offline"}; 
                        AlertDialog.Builder builder = new AlertDialog.Builder(SetPrediction.this); 


                        builder.setTitle("Change Status"); 
                        builder.setItems(items, new DialogInterface.OnClickListener() 
                        { 
                            public void onClick(DialogInterface dialog, int item) 
                            { 
                                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); 
                            } 
                        });

                        AlertDialog alert = builder.create(); 
                        alert.show();
                    } 
                });
            }
        });
    }


    public String removeSpaces(String s) 
    {  
        StringTokenizer st = new StringTokenizer(s," ",false);
        String t="";
        while (st.hasMoreElements()) t += st.nextElement();
        return t;
    }

The problem is in the listView.setOnItemClickListener(new OnItemClickListener() event. That the part that doesn't work.

2012-04-05 02:26
by Zolt


1

Check out this:

public void showAlertDialog(String title, String message, Context context)
{
    final AlertDialog alertDialog = new AlertDialog.Builder(context).create();

    alertDialog.setTitle(title);
    alertDialog.setMessage(message);
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            alertDialog.dismiss();
        }
    });

    alertDialog.show();
}
2012-04-05 10:11
by Ravi1187342
Do I put this outside the class definition? So after the removeSpaces method, for example? And then call showAlertDialog from within onItemClick - Zolt 2012-04-05 16:22
If i understand you want to reuse this funtion by putting it in separate class ? if that , you can make a class named MyUtil and put it inside it. you can call it from any activity within onItemClick by passing the parameters - Ravi1187342 2012-04-05 16:43
The Alert message still does not show. I put your code in a class as follows public class Util extends Activity {} and then from within onItemClick in SetPrediction I added Util.showAlertDialog("testTitle", "testMssage", predictView.getContext()); but nothing appears - Zolt 2012-04-06 17:51
I'm pretty much giving up on this problem. There seems to be no solution after two days of research. The problem is with my program structuring. I have a listview event within a button event, and to make things more complex, there's activity group involved. Everything I've tried either crashes the program or doesn't show the dialog. But your code does work in regular, more normal situations. So thanks for the help anyways - Zolt 2012-04-07 20:56
@Ravi alertdialog doesn't have dismiss method. Where did you get that - The_Martian 2015-10-22 21:41


0

Try using these code instead of your code just a minor change:::

AlertDialog.Builder builder = new AlertDialog.Builder(SetPrediction.this).create();
builder.setTitle("Change Status"); 
builder.setItems(items, new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int item){ 
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); 
} 
});
builder.show();

also import the following :::

import android.app.AlertDialog; import android.content.DialogInterface;

2012-04-05 02:52
by Shankar Agarwal
This doesn't build. Line 3 gives the following error: The method setItems(CharSequence[], new DialogInterface.OnClickListener(){}) is undefined for the type AlertDialogZolt 2012-04-06 17:55
add the import - Shankar Agarwal 2012-04-07 03:02
Ads