gpt4 book ai didi

java - OnListItemClick 只工作一次

转载 作者:行者123 更新时间:2023-11-29 21:54:42 26 4
gpt4 key购买 nike

我正在处理一个 ListView ,当您单击一个项目时,它会显示一个带有 2 个选项的警报对话框,在他们选择一个选项后,它会将他们带回 ListView ,在那里他们可以再次选择另一个选项。出于某种原因,一旦我单击一个项目,如果我单击肯定按钮(其中包含代码的按钮),然后尝试单击另一个项目,则不会弹出警报对话框。有谁知道为什么会这样?代码如下:

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

最佳答案

您的 saveLevel() 函数做了很多不适合 UI 线程的事情(网络访问、文件访问)。您应该将这些东西分离到后台线程中,最好使用 AsyncTask ( tutorial )。

当您尝试单击另一个列表项时,很可能您的程序仍停留在 saveLevel() 中。如果您想检查这个假设是否真的成立,只需在 saveLevel() 的开头放置一个 return; 行,然后查看您的对话框是否按预期弹出。

关于java - OnListItemClick 只工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13537697/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com