2015-12-06 51 views
0

我有一個由mysql數據庫中的數據填充的listview。它工作正常,但當我選擇一個項目,然後按回,以前的列表視圖fecth再次從數據庫中複製我的列表視圖中的項目的數據。Android ListView按下按鈕後複製數據

這裏是我的代碼:

public class CityPage extends Activity{ 
    Activity context; 
HttpPost httppost; 
    StringBuffer buffer; 
    HttpResponse response; 
    HttpClient httpclient; 
    ProgressDialog pd; 
    CityAdapter cityAdapter; 
    ListView listCity; 
    ArrayList<City> records; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_city_page); 

    context = this; 
    records = new ArrayList<City>(); 
    listCity = (ListView) findViewById(R.id.cities); 
    cityAdapter = new CityAdapter(context, R.layout.city_layout, R.id.city_name, records); 
    listCity.setAdapter(cityAdapter); 
    listCity.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
@Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    Intent myIntent = new Intent(view.getContext(),City_attractions.class); 
    Toast.makeText(CityPage.this, "Opening", Toast.LENGTH_LONG).show(); 
    String info1 = records.get(position).getCityName(); 
    String info2 = records.get(position).getDescription(); 
    myIntent.putExtra("info1", info1); 
    myIntent.putExtra("info2", info2); 

    startActivity(myIntent); 
    } 
    }); 
    } 


    @Override 
    protected void onStart() { 
    super.onStart(); 
    fetchCity fetch = new fetchCity(); 
    fetch.execute(); 

    } 

    private class fetchCity extends AsyncTask<Void, Void, Void> { 

protected void onPreExecute() { 
    super.onPreExecute(); 
} 

protected Void doInBackground(Void... params) { 

    InputStream is = null; 
    String result = ""; 

    try { 
     httpclient = new DefaultHttpClient(); 
     httppost = new HttpPost("http://iguideph-001-site1.btempurl.com/getcity.php"); 
     response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 

     // Get our response as a String. 
     is = entity.getContent(); 

    } catch (Exception e) { 
     if (pd != null) 
      pd.dismiss(); //close the dialog if error occurs 
     Log.e("ERROR", e.getMessage()); 

    } 

    //convert response to string 

    try { 

     BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     result = sb.toString(); 

    } catch (Exception e) { 
     Log.e("ERROR", "Error converting result " + e.toString()); 

    } 

    //parse json data 
    try { 
     // Remove unexpected characters that might be added to beginning of the string 
     result = result.substring(result.indexOf("")); 
     JSONArray jArray = new JSONArray(result); 

     for (int i = 0; i < jArray.length(); i++) { 
      JSONObject json_data = jArray.getJSONObject(i); 
      City p = new City(); 
      p.setCityName(json_data.getString("place_name")); 
      p.setDescription(json_data.getString("description")); 

      records.add(p); 
     } 
    } catch (Exception e) { 
     Log.e("ERROR", "Error pasting data " + e.toString()); 

    } 

    return null; 
} 


protected void onPostExecute(Void result) { 
    if (pd != null) pd.dismiss(); //close dialog 
    Log.e("size", records.size() + ""); 
    cityAdapter.notifyDataSetChanged(); //notify the ListView to get new records 
} 
    } 
    } 
+0

將您的AsyncTask實例化並執行到'onCreate()'。 –

+0

我已將其移至我的創建者處,並使其不私密。但是我在Onstart上有一個錯誤。它正在尋找fetchcity。 – bluelover

回答

1

嘗試從的OnStart()刪除這些行,並把它們裏面的OnCreate()函數

fetchCity fetch = new fetchCity(); 
fetch.execute(); 

祝你好運!

+0

謝謝@ minos23!它適用於我:)這樣一個偉大的幫助:) – bluelover