1

特定の日の勤務表を取得するために介護者が使用するアプリがあります。ユーザーは日付ピッカーで日付を設定します。この日付は、NfcScannerActivity というアクティビティに渡されます。このアクティビティは、その日付を使用して Web サービスを呼び出し、勤務表を取得します。勤務表データは配列です。NfcScannerActivity は、この配列を GetRota に渡します。これは、勤務表データを表示する配列アダプターを持つアクティビティです。

GetRota の getView メソッドで特定の日のデータがないかどうかを確認するまで、これはすべて正常に機能します。この時点で、この事実を述べてユーザーに乾杯し、別の日付 (できれば有効な日付) で NfcScannerActivity を再度呼び出します。データがない場合、ユーザーが 2 回トーストされるため、getView メソッドが 2 回実行されるように見えます。どうしてこれなの?

NfcScannerActivity からのスニペット:

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);

    }







//////////snippet of async result of webservice call
...........
..........
 @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{...........
              ...........

.

GetRota からのスニペット、勤務表を示すアクティビティ

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



@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());

..............
...............





/////////adapter class in getrota

private class MySimpleArrayAdapter extends ArrayAdapter<String> {
        private final Context context;
        private final ArrayList<?> list;

        public MySimpleArrayAdapter(Context context, ArrayList<?> list) {

            super(context, R.layout.rotarowlayout);
            Log.e(TAG, "inside adapter constructor");
            this.context = context;
            this.list = list;


        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {


            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View rowView = inflater.inflate(R.layout.rotarowlayout, parent,
                    false);

            TextView startTime = (TextView) rowView
                    .findViewById(R.id.rowstarttime);
            TextView duration = (TextView) rowView
                    .findViewById(R.id.rowduration);
            TextView status = (TextView) rowView.findViewById(R.id.rowstatus);
            TextView name = (TextView) rowView.findViewById(R.id.rowclientname);
            //ImageView imageView = (ImageView)rowView.findViewById(R.id.rowimagestatus);


            String record = list.get(position).toString();
            rowView.setTag(record);
            String[] itemsInRecord = record.split(",");
            Log.e(TAG, "itemin record = " + itemsInRecord.length);
            String[] recordItem = new String[itemsInRecord.length];

            for (int x = 0; x < itemsInRecord.length; x++) {

                recordItem[x] = itemsInRecord[x];

            }


            if(recordItem[9].toString().trim().equalsIgnoreCase("Out of range]")){

                Toast.makeText(GetRota.this, "No rota available", Toast.LENGTH_LONG).show();
                nfcscannerapplication.setGobalDateTime(new DateTime());
                //onBackPressed();

                DateTime globalDateTime = nfcscannerapplication.getglobalDateTime();
                DateTimeFormatter fmt = DateTimeFormat.forPattern("d-MMM-Y");
                String formattedglobalDateTime = fmt.print(globalDateTime);
                Intent i = new Intent(GetRota.this, NfcscannerActivity.class);
                i.putExtra("nextRota", formattedglobalDateTime);
                i.setAction("NEXT_ROTA"); 
                startActivity(i);


            }else if(recordItem[0].toString().trim().equalsIgnoreCase("[nodata")){

                Toast.makeText(GetRota.this, "You have no calls", Toast.LENGTH_LONG).show();
                Log.e(TAG, "you have no calls");

                nfcscannerapplication.setGobalDateTime(new DateTime());
                //onBackPressed();

                DateTime globalDateTime = nfcscannerapplication.getglobalDateTime();
                DateTimeFormatter fmt = DateTimeFormat.forPattern("d-MMM-Y");
                String formattedglobalDateTime = fmt.print(globalDateTime);
                Intent i = new Intent(GetRota.this, NfcscannerActivity.class);
                i.putExtra("nextRota", formattedglobalDateTime);
                i.setAction("NEXT_ROTA"); 
                startActivity(i);
4

2 に答える 2

1

getView()の呼び出しを制御することはできません。arrayadapterは、データビューとリストビューの間のブリッジとして機能します。リストビューが読み込まれたり、更新されたり、スクロールなどが発生したりすると、リストビューは配列アダプタのgetviewメソッドを呼び出します。したがって、 getView()がいつ呼び出されるかを制御することはできません。
さらに、リストの各項目に対してgetView()が呼び出されます。したがって、持っているアイテムが2つある場合 recordItem[0] as no data、トーストは2回表示されます。

recorditemは、 ArrayListlistの特定の位置から作成される配列です。

recorditem=list.get(position).split();

したがって、リストビューのrow1はrecorditem配列を保持し、row2は別のrecorditem配列を保持します。したがって、両方の行のrecorditem [0]にデータがない場合、Toastは複数回表示されます。

于 2012-11-07T11:22:15.170 に答える
0

これは、getView が制御できないためです。Android システムによって決定されるように、一貫してビューを更新するように設計されています。

おそらく、トーストをコード内の別の場所に移動できます。onResume または onActivityResult に勤務表がないかどうかを確認できます。一般に、getView メソッドの ArrayList 内の特定の項目のみを考慮する必要があります。

于 2012-11-07T11:30:45.130 に答える