java Android 对话框未显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10021876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android dialog not showing
提问by Zolt
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?
谁会想到要显示一个对话框会如此困难。在 C# 中,这很容易。我到处寻找帮助,每个提示似乎都给我带来了麻烦而不是好处。有人可以看看下面的代码并告诉我我可能出错的地方吗?
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.
我想要的很简单。用户点击按钮。然后出现列表。然后用户单击列表中的项目之一,出现对话框。listview 事件中 onItemClick 中的代码来自别人。它显然适用于那个人。我一直点击,什么也没看到。我愿意接受有关如何修复它或新代码的建议。抱歉,日志没有错误消息。
Please be specific about where I put the code as I'm an Android noob. And thank you in advance!
请具体说明我把代码放在哪里,因为我是一个 Android 菜鸟。并提前致谢!
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.
问题出在listView.setOnItemClickListener(new OnItemClickListener()
事件上。那部分不起作用。
采纳答案by Ravi1187342
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();
}
回答by Shankar Agarwal
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;
导入 android.app.AlertDialog; 导入 android.content.DialogInterface;