后退按钮按列表视图项在android中变为double

时间:2015-08-12 06:27:54

标签: android

这是我的异步任务:

class download extends AsyncTask<Void, Void, Void> {

            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                // progress.setVisibility(View.VISIBLE);
                pDialog = new ProgressDialog(EmployeeVisit.this);
                pDialog.setTitle("While Data Fetching");
                pDialog.setMessage("Please wait...");
                pDialog.setCanceledOnTouchOutside(false);
                pDialog.show();
                super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... params) {
                // TODO Auto-generated method stub

                try {
                    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                    request.addProperty("Employee", "EMP01");
                    Bean C = new Bean();
                    PropertyInfo pi = new PropertyInfo();
                    pi.setName("Bean");
                    pi.setValue(C);
                    pi.setType(C.getClass());
                    request.addProperty(pi);
                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                            SoapEnvelope.VER11);
                    envelope.dotNet = true;
                    envelope.setOutputSoapObject(request);
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                    androidHttpTransport.debug = true;
                    androidHttpTransport.call(SOAP_ACTION, envelope);
                    SoapObject result = (SoapObject) envelope.getResponse();
                    envelope.addMapping(NAMESPACE, "Panchayat",
                            new Bean().getClass());
                    androidHttpTransport.call(SOAP_ACTION, envelope);
                    Bean[] personobj = new Bean[result.getPropertyCount()];
                    Bean beanobj = new Bean();
                    for (int j = 0; j < personobj.length; j++) {
                        SoapObject pii = (SoapObject) result.getProperty(j);
                        beanobj.City = pii.getProperty(0).toString();
                        beanobj.EmployeeID = pii.getProperty(1).toString();
                        beanobj.RouteNumber = pii.getProperty(2).toString();
                        beanobj.SpokeCode = pii.getProperty(3).toString();
                        beanobj.VisitMasterId = pii.getProperty(4).toString();
                        personobj[j] = beanobj;
                        city = beanobj.City;
                        employyeid = beanobj.EmployeeID;
                        routenumber = beanobj.RouteNumber;
                        spokecode = beanobj.SpokeCode;
                        visitmaster = beanobj.VisitMasterId;
                        stringArray.add(city);
                        routestringArray.add(routenumber);

                        hashmap.put(city, visitmaster);
                        hashmap1.put(city, routenumber);
                    }
                    arr = new int[routestringArray.size()];

                    for (int i = 0; i < routestringArray.size(); i++) {
                        arr[i] = Integer.parseInt(routestringArray.get(i));// this
                                                                            // will
                                                                            // work!!
                        System.out.println(arr[i]);
                    }

                    leastroutenumber = leastnumber(arr);


                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (XmlPullParserException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                return null;

            }

            @Override
            protected void onPostExecute(Void result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);

                String array[] = new String[stringArray.size()];

                for (int j = 0; j < stringArray.size(); j++) {
                    array[j] = stringArray.get(j);
                }

                Myadapter adMyadapter = new Myadapter(EmployeeVisit.this, array);
                listview.setAdapter(adMyadapter);
                pDialog.dismiss();

            }

        }

我在Oncreate中调用它{new download()。execute}和onressume(new download()。execute)  两种方法但是当第一次启动应用程序时,然后两个方法调用请告诉我我在哪里做错了:

public class EmployeeVisit extends Activity {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

new download().execute();
}
} 

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();


            new download().execute();

    }

请建议我如何调用此

2 个答案:

答案 0 :(得分:1)

请勿在{{1​​}}和download().execute();功能中致电onCreate。仅在onResume中调用此方法。

答案 1 :(得分:0)

尝试使用“onSaveInstanceState”:

@Override
public void onSaveInstanceState(Bundle outState) {
   super.onSaveInstanceState(outState);
   outState.putString("message", "Activity was startetd once");
}

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  if( savedInstanceState == null ) {
     new download().execute();
  }
}

onSaveInstanceState Doc (Google)

另一种方法可能是使用“startActivityForResult”

从活动A开始活动B:

Intent i = new Intent(this, B.class);
startActivityForResult(i, 1);

像这样完成活动B:

Intent returnIntent = new Intent();
// optional intent, if you want give something back to the starter Activity
returnIntent.putExtra("result",result);

// If no intent needed -> setResult(RESULT_OK);
setResult(RESULT_OK,returnIntent);
finish();

再次将结果记录在活动A中:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {
            if(resultCode == RESULT_OK){
                new download().execute();
            }
            if (resultCode == RESULT_CANCELED) {
                //Write your code if there's no result
            }
        }
    }

所以你可以删除

new download().execute();

onResume()