我有一個應用程序,用於照顧護士的輪迴。一旦護理人員登錄,他/她可以點擊一個名爲getRota的按鈕。這個按鈕調用一個webservice並獲取當天的rota,同時日期也傳遞給這個方法。一旦在rota頁面上顯示的rota顯示在列表視圖中,我已經用3個按鈕覆蓋了activity的選項菜單,接下來,前一個選擇一天。前2個按鈕會添加一天或從dateTime中刪除一天,然後將其傳回給調用類,以便使用新的日期時間再次調用webservice。android listview適配器不顯示正確的數據
所有這些都可以正常工作,直到您在查看rota時按下後退鍵。當後退鍵被按下時,它會返回到可以單擊getRota的調用活動,在此方法內,我始終將dateTime設置爲今天。問題是,當用戶點擊獲取旋轉,然後點擊下一個,然後返回鍵,然後getrota,今天的日期應該顯示在列表視圖中,但第二天的旋轉顯示。這就像listview沒有被當前日子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();
}
}
}