Android对话框没有显示

时间:2012-04-05 02:26:01

标签: java android android-layout dialog

谁会想到要显示一个对话框会很困难。在C#中它很容易。我已经全神贯过地寻求帮助,每一个提示似乎都给我带来了麻烦而不是好事。有人可以看看下面的代码并告诉我哪里可能出错了吗?

我想要的很简单。用户点击按钮。然后出现列表。然后,用户单击列表中的一个项目,然后会出现对话框。 listview事件中onItemClick中的代码来自其他人。它显然适用于那个人。我一直点击,什么都看不到。我对如何解决它或新代码的建议持开放态度。抱歉,日志没有错误消息。

请具体说明我将代码放在哪里,因为我是一个Android noob。并提前谢谢你!

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;
    }

问题出在listView.setOnItemClickListener(new OnItemClickListener()事件中。那个没有用的部分。

2 个答案:

答案 0 :(得分:1)

看看这个:

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();
}

答案 1 :(得分:0)

尝试使用这些代码而不是代码只是一个小改动:::

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();

还导入以下:::

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