0

我有一个用于为照顾者获取轮班表的应用程序。护理人员登录后,他/她可以单击名为 getRota 的按钮。此按钮调用 Web 服务并获取当天的轮播表,因为日期也传递给此方法。一旦在列表视图中显示列表的列表页面上,我已经用 3 个按钮覆盖了该活动的选项菜单,下一个,上一个和选择一天。第一个 2 按钮从 dateTime 中添加一天或删除一天,然后将其传递回调用类以使用新的 datetime 再次调用 web 服务。

所有这些都可以正常工作,直到您在查看列表时按返回键。当按下返回键时,它会返回调用活动,您可以在其中单击 getRota,在此方法中,我总是将 dateTime 设置为今天。问题是,当用户点击 get rota,然后点击 next,然后返回键然后 getrota,今天的日期应该显示在列表视图中,但显示第二天的 rota。就像列表视图没有使用当前日期更新。

这是一些代码,如果需要,我可以提供更多。

public class GetRota extends NfcBaseActivity implements OnItemClickListener {

    private static final String TAG = GetRota.class.getSimpleName();
    ListView listView;
    Intent intent;
    String callID;

    NfcScannerApplication nfcscannerapplication;
    ArrayList<?> array;
    String needName = "";
    MySimpleArrayAdapter arrayAdapter;
    private DatePicker dpResult;
    private int year;
    private int month;
    private int day;
    String statusField;
    static final int DATE_DIALOG_ID = 999;  
    TextView textViewDate; 

    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        super.onNewIntent(intent);
    }

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

        nfcscannerapplication = (NfcScannerApplication) getApplication();
        //set titlebar to carer's name
        Cursor cursorCarerName = nfcscannerapplication.loginValidate.queryAllFromCarer();
        cursorCarerName.moveToLast();
        String carerTitleName = cursorCarerName.getString(cursorCarerName.getColumnIndex(LoginValidate.C_CARER_FIRSTNAME)) + " " + cursorCarerName.getString(cursorCarerName.getColumnIndex(LoginValidate.C_CARER_LASTNAME)) ;
        setTitle(carerTitleName + " is currently logged in");
        listView = (ListView) findViewById(R.id.rotalist);
        textViewDate = (TextView)findViewById(R.id.textviewdate);
        Log.e(TAG, "textview = "+textViewDate);
        listView.setAdapter(arrayAdapter);
        listView.setOnItemClickListener(this);
    }// end of onCreate

    @Override
    public void onBackPressed() {
        Log.e(TAG, "onBack pressed globaldatetime = " + nfcscannerapplication.getglobalDateTime());
        super.onBackPressed();
    }

    @Override
    protected void onResume(){
        super.onResume();
        Log.e(TAG, "global date in onresume getrota = " + nfcscannerapplication.getglobalDateTime());
        array = (ArrayList<String[]>)getIntent().getBundleExtra("rotaArrayBundle").get("rotaArray");
        Log.e(TAG, "array size in onresume = " + array.size());
        if(array.size() == 0){
            //then needname must be out of range, toast user with no rota available
        DateTime unavailableDate = nfcscannerapplication.getglobalDateTime();
        DateTimeFormatter fmt2 = DateTimeFormat.forPattern("E dd MMM");
        String unavailStringDate = fmt2.print(unavailableDate);
        Log.e(TAG, "no rota!!!!!!!!!!!!!!!!");
        AlertDialog alertDialog = new AlertDialog.Builder(GetRota.this).create();
        alertDialog.setTitle("No Rota Available ");
        alertDialog.setMessage("Unable To View Rota For  " +"\n" + unavailStringDate);
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {

                          onBackPressed();

                    } }); 

                alertDialog.show();

            }
            if (arrayAdapter == null){
                MySimpleArrayAdapter arrayAdapter = new MySimpleArrayAdapter(this, array);

                listView.setAdapter(arrayAdapter);
            }
        }


        private MySimpleArrayAdapter getListAdapter() {

            return arrayAdapter;
        }

        public void setCurrentDateOnView() {

            dpResult = (DatePicker) findViewById(R.id.datepicker1);

            final Calendar c = Calendar.getInstance();
            year = c.get(Calendar.YEAR);
            month = c.get(Calendar.MONTH);
            day = c.get(Calendar.DAY_OF_MONTH);


        }

        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.menurotadetails, menu);
            return true;
        }

        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {


            case R.id.previous:


                DateTime dateTime = nfcscannerapplication.getglobalDateTime();
                DateTime dateTimeMinusOne = dateTime.minusDays(1);
                nfcscannerapplication.setGobalDateTime(dateTimeMinusOne);

                DateTimeFormatter fmt2 = DateTimeFormat.forPattern("d-MMM-Y");
                String previousDay = fmt2.print(dateTimeMinusOne);

                Intent i2 = new Intent(this, NfcscannerActivity.class);
                i2.putExtra("nextRota", previousDay);
                i2.setAction("NEXT_ROTA");
                startActivity(i2);

                return true;


            case R.id.next:

                DateTime dateTime2 = nfcscannerapplication.getglobalDateTime();
                DateTime dateTimePlusOne = dateTime2.plusDays(1);
                nfcscannerapplication.setGobalDateTime(dateTimePlusOne);

                DateTimeFormatter fmt = DateTimeFormat.forPattern("d-MMM-Y");
                String nextDay = fmt.print(dateTimePlusOne);

                Intent i = new Intent(this, NfcscannerActivity.class);
                i.putExtra("nextRota", nextDay);
                i.setAction("NEXT_ROTA");
                startActivity(i);

                return true;

            case R.id.today:
                setCurrentDateOnView();
                showDialog(DATE_DIALOG_ID);
                return true;

            default:

                return super.onOptionsItemSelected(item);
            }
        }


//adapter stuff...

.

在调用类(NfcscannerActivity)

 Button getRota = (Button)findViewById(R.id.buttongetrota);

            getRota.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Log.e(TAG, "onclicked getRota");






                    DateTime now = new DateTime();
                    nfcscannerapplication.setGobalDateTime(now);
                    Log.e(TAG, "now in getrota method in nfcact = "+ now);
                    DateTimeFormatter fmt = DateTimeFormat.forPattern("d-MMM-Y");
                    String formattedNow = fmt.print(now);
                    String[] params = new String[]{nfcscannerapplication.getCarerID(), formattedNow}; 
                    AsyncGetRota agr = new AsyncGetRota();
                    agr.execute(params); 

                     }

                }// end of onclick
            });


some intent processing......
if(intent.getAction().equalsIgnoreCase("NEXT_ROTA")){

            Log.e(TAG, "next rota action");
            String date = intent.getStringExtra("nextRota");

            getNextRota(date);
        } 



private void getNextRota(String stringExtra) {

    String[] params = new String[]{nfcscannerapplication.getCarerID(), stringExtra}; 
    AsyncGetRota agr = new AsyncGetRota();
    agr.execute(params);

    }



private class AsyncGetRota extends AsyncTask<String, Void, Void> {

        ProgressDialog progressDialog;
        Boolean isRotaArrayNull = false;

        @Override
        protected void onPreExecute()
        {
            progressDialog= ProgressDialog.show(NfcscannerActivity.this, 
                    "Connecting to Server"," retrieving rota...", true);


        };      


        @Override
        protected Void doInBackground(String... params) {

            try {
                Log.e(TAG, "inside doInBackground");

                Log.e(TAG, "now in doinbackground = " + params[1]);
                rotaArray = nfcscannerapplication.loginWebservice.getRota(params[0], params[1]);
                //nfcscannerapplication.loginWebservice.getRota(params[0], params[1]);

               if (rotaArray == null){
                   Log.e(TAG, "about to call onstart");
                   isRotaArrayNull = true;
               }

            } catch (Exception e) {



               e.printStackTrace();


            }
            return null;

        }

        @Override
        protected void onPostExecute(Void result)
        {
            super.onPostExecute(result);
            if(progressDialog != null)
            progressDialog.dismiss();

           if(isRotaArrayNull == false){

            Intent intent = new Intent(NfcscannerActivity.this,
                            GetRota.class);
             Bundle b = new Bundle();
             b.putSerializable("rotaArray", rotaArray);

             intent.putExtra("rotaArrayBundle", b);
             startActivity(intent);
        }else{

            AlertDialog alertDialog = new AlertDialog.Builder(NfcscannerActivity.this).create();
            alertDialog.setTitle("Signal Test");
            alertDialog.setMessage("No Phone Signal");

             alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {

                      onStart();
                } }); 



             alertDialog.show();

        }

        }


    }
4

1 回答 1

0

解决了。我将以下内容添加到清单中

<activity
            android:name=".GetRota"
            android:launchMode="singleTask"
            android:screenOrientation="portrait" >
        </activity>

我不是 100% 为什么这会有所作为,但是将 singleTop 更改为 singleTask 已经解决了

于 2012-10-22T12:47:21.410 回答