我正在做一個列表視圖,當你點擊一個項目時,它顯示一個alertdialog有兩個選項,並且在他們選擇一個選項後,它將他們帶回列表視圖,在那裏他們可以再次使用另一個選擇。出於某種原因,一旦我點擊一個項目,如果我點擊肯定按鈕(其中的代碼),然後嘗試點擊另一個項目,警報對話框不會彈出。有誰知道爲什麼會發生?代碼如下:OnListItemClick只能工作一次
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());
}
}
雲你檢查logcat,看看它是否引發任何異常或不? –
有沒有例外 –