Reputation: 305
I am working on a listview that when you click an item, it shows an alertdialog with 2 options, and after they pick an option it takes them back to the listview where they can do it again with another selection. For some reason once I click an item, if I click the positive button (the one with code in it) then try and click on another item the alert dialog doesn't pop up. Does anyone know why this would happen? The code is below:
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);
}
If you need anything other info let me know! Thanks in advance!
William
Edit
From what I can tell, it is happening in the saveLevel function, I am posting it below
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());
}
}
Upvotes: 1
Views: 154
Reputation: 23508
Your saveLevel()
function does a lot of things (network access, file access), that are not suited well for UI thread. You should separate these things into a background thread, preferably using AsyncTask
(tutorial).
Most probably your program is still stuck in saveLevel()
when you're trying to click another list item. If you want to check if this assumption is really true, just put a return;
line in the beginning of saveLevel()
and see if your dialog box pops up as intended.
Upvotes: 2