我正在制作一个列表视图,当你点击一个项目时,它会显示一个带有2个选项的alertdialog,在他们选择一个选项后,它会将它们带回列表视图,在那里他们可以再次使用其他选项。出于某种原因,一旦我点击一个项目,如果我点击肯定按钮(其中包含代码的那个),然后尝试点击另一个项目,警报对话框不会弹出。有谁知道为什么会这样?代码如下:
protected void onListItemClick(ListView l, View v, int position, long id) {
final int i = position;
try {
new AlertDialog.Builder(this)
.setTitle("Download")
.setMessage("You have selected the level " + jArray.getJSONObject(i).getString("level_name") + ". Would you like to save it?")
.setPositiveButton("Save", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
try {
saveLevel(jArray.getJSONObject(i).getString("level_name"),jArray.getJSONObject(i).getInt("id"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return;
}
})
.setNegativeButton("Cancel",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
return;
}
})
.show();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onListItemClick(l, v, position, id);
}
如果您需要任何其他信息,请告诉我们!提前谢谢!
威廉
修改的
据我所知,它发生在saveLevel函数中,我将它发布在
之下public void saveLevel(String i, int id)
{
InputStream is = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("uid","demo"));
String result = "";
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("www.example.com/stuff.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
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();
jArray = new JSONArray(result);
File exst = Environment.getExternalStorageDirectory();
String exstPath = exst.getPath();
File filePath = new File(exstPath+"/Platformer/Downloaded");
boolean success = filePath.mkdir();
String path = filePath.getAbsolutePath() + "/" + i + ".xml";
File newxmlfile = new File(path);
try
{
newxmlfile.createNewFile();
}
catch (IOException e)
{
e.printStackTrace();
}
FileOutputStream fileos = null;
try
{
fileos = new FileOutputStream(newxmlfile);
}
catch(FileNotFoundException e)
{
Log.e("FileNotFoundException", "can't create FileOutputStream");
return;
}
OutputStreamWriter output = new OutputStreamWriter(fileos);
output.write(jArray.getJSONObject(0).getString("level_data"));
output.flush();
output.close();
}
catch(Exception e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
答案 0 :(得分:2)
您的saveLevel()
函数执行了许多不适合UI线程的事情(网络访问,文件访问)。您应该将这些内容分成后台主题,最好使用AsyncTask
(tutorial)。
当您尝试点击其他列表项时,很可能您的程序仍然卡在saveLevel()
中。如果你想检查这个假设是否真的如此,只需在return;
的开头放一条saveLevel()
行,看看你的对话框是否按预期弹出。