为什么ArrayAdapter类中的getView执行两次?

时间:2012-11-07 10:57:14

标签: android view android-arrayadapter

我有一个应用程序,护理人员使用它来获取特定日期的rota。用户在日期选择器中设置日期。此日期将传递给名为NfcScannerActivity的活动。此活动使用该日期调用Web服务来获取rota。 rota数据是一个数组。 NfcScannerActivity将此数组传递给GetRota,GetRota是一个具有数组适配器的活动,用于显示rota数据。

所有这一切都正常,直到在GetRota的getView方法中我检查特定日期是否没有数据。此时,我向用户致敬说明这一事实,然后用另一个日期(希望是有效日期)重新调用NfcScannerActivity。如果没有数据,则getView方法似乎执行两次,因为用户需要两次烘烤。这是为什么?

来自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的

片段,显示rota的活动

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

2 个答案:

答案 0 :(得分:1)

您无法控制 getView()的调用。 arrayadapter充当数据和列表视图之间的桥梁。当列表视图加载,刷新或有一些滚动等时,列表视图调用数组适配器的get view方法。因此,您无法控制何时调用 getView() 此外,为列表的每个项调用getView()。因此,如果有两个项目有recordItem[0] as no data,则吐司会出现两次。

recorditem 是从 ArrayList 列表的特定位置创建的数组。

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

因此listview的row1将保存一个recorditem数组,row2将保存另一个recorditem数组。因此,如果两行的 recorditem [0] 都有无数据,则会多次显示Toast。

答案 1 :(得分:0)

这是因为getView是我们无法控制的。它采用这种方式设计,可以持续刷新视图,具体取决于Android系统。

也许您可以在代码中的其他位置移动toast。您可以检查onResume或onActivityResult中是否没有rota。通常,您应该只关注getView方法中ArrayList中的特定项。

相关问题