1

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

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

1 回答 1

2

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

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

于 2012-11-24T03:06:49.667 回答