我正在使用一个列表视图,当您单击一个项目时,它会显示带有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());
    }
}

最佳答案

您的saveLevel()函数完成了很多事情(网络访问,文件访问),这些都不适合UI线程。您应该将这些东西分成一个后台线程,最好使用AsyncTasktutorial)。

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

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

10-12 02:16