繁体   English   中英

按下返回按钮后,Android ListView复制数据

[英]Android ListView duplicates data after pressing back button

我有一个由mysql数据库中的数据填充的listview。 它工作正常,但是当我选择一个项目然后按back时,上一个listview会再次从数据库复制我的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
}
  }
   }

尝试从onstart()中删除这些行,并将其放入oncreate()函数中

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

祝好运 !

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM