ListView在滚动时崩溃App使用ViewHolder

时间:2013-04-02 00:00:03

标签: android listview android-listview custom-adapter

当我在ListView中向上和向下滚动几次时,我的应用程序崩溃了,这是由一个扩展baseAdapter的customAdapter提供的。

还有一个问题,当我向上和向下滚动时,行中的信息有时会改变位置,有时会消失,这只是

logcat的

04-02 00:30:25.390: I/System.out(4461): Not a DRM File, opening notmally
04-02 00:30:25.445: I/System.out(4461): buffer returned 
04-02 00:30:25.531: I/dalvikvm-heap(4461): Forcing collection of SoftReferences for 12582928-byte allocation
04-02 00:30:25.570: E/dalvikvm-heap(4461): Out of memory on a 12582928-byte allocation.
04-02 00:30:25.570: I/dalvikvm(4461): "main" prio=5 tid=1 RUNNABLE
04-02 00:30:25.570: I/dalvikvm(4461):   | group="main" sCount=0 dsCount=0 obj=0x41135508 self=0x40f0a930
04-02 00:30:25.570: I/dalvikvm(4461):   | sysTid=4461 nice=0 sched=0/0 cgrp=apps handle=1074736944
04-02 00:30:25.570: I/dalvikvm(4461):   | schedstat=( 11607757611 1931762655 12518 ) utm=1084 stm=75 core=1
04-02 00:30:25.570: I/dalvikvm(4461):   at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
04-02 00:30:25.570: I/dalvikvm(4461):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:619)
04-02 00:30:25.570: I/dalvikvm(4461):   at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:385)
04-02 00:30:25.570: I/dalvikvm(4461):   at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:418)
04-02 00:30:25.570: I/dalvikvm(4461):   at com.android.moha.MainActivity$MyCustomAdapter.getView(MainActivity.java:754)
04-02 00:30:25.570: I/dalvikvm(4461):   at android.widget.AbsListView.obtainView(AbsListView.java:2445)
04-02 00:30:25.570: I/dalvikvm(4461):   at android.widget.ListView.makeAndAddView(ListView.java:1769)
04-02 00:30:25.570: I/dalvikvm(4461):   at android.widget.ListView.fillUp(ListView.java:706)
04-02 00:30:25.570: I/dalvikvm(4461):   at android.widget.ListView.fillGap(ListView.java:645)
04-02 00:30:25.570: I/dalvikvm(4461):   at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5530)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3413)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.widget.AbsListView.onTouchEvent(AbsListView.java:3906)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.View.dispatchTouchEvent(View.java:7340)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2179)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1914)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1928)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1928)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1928)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1928)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1928)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1928)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1928)
04-02 00:30:25.578: I/dalvikvm(4461):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2113)
04-02 00:30:25.578: I/dalvikvm(4461):   at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1466)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.app.Activity.dispatchTouchEvent(Activity.java:2468)
04-02 00:30:25.578: I/dalvikvm(4461):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2061)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.View.dispatchPointerEvent(View.java:7525)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3368)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3300)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4392)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4370)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4474)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4442)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4493)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.Choreographer.doCallbacks(Choreographer.java:555)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.Choreographer.doFrame(Choreographer.java:523)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.os.Handler.handleCallback(Handler.java:615)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.os.Handler.dispatchMessage(Handler.java:92)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.os.Looper.loop(Looper.java:137)
04-02 00:30:25.578: I/dalvikvm(4461):   at android.app.ActivityThread.main(ActivityThread.java:4895)
04-02 00:30:25.578: I/dalvikvm(4461):   at java.lang.reflect.Method.invokeNative(Native Method)
04-02 00:30:25.578: I/dalvikvm(4461):   at java.lang.reflect.Method.invoke(Method.java:511)
04-02 00:30:25.578: I/dalvikvm(4461):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
04-02 00:30:25.578: I/dalvikvm(4461):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
04-02 00:30:25.578: I/dalvikvm(4461):   at dalvik.system.NativeStart.main(Native Method)
04-02 00:30:25.617: E/InputEventReceiver(4461): Exception dispatching input event.

CustomAdapter getView

  @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        System.out.println("getView " + position + " " + convertView);
        final ViewHolder holder;
        if (convertView == null) {
            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(l, null);
            holder  = new ViewHolder();

            holder.id = (TextView)convertView.findViewById(R.id.id);
            holder.name = (TextView)convertView.findViewById(R.id.name);
            holder.price = (TextView)convertView.findViewById(R.id.price);
            holder.description = (TextView)convertView.findViewById(R.id.description);
            holder.button = (Button) convertView.findViewById(R.id.order);
            holder.quantity = (TextView) convertView.findViewById(R.id.quantity);

            if (a == itemsList){
            holder.img = (ImageView) convertView.findViewById(R.id.menu_icon);

            }


            convertView.setTag(holder);
        }

        else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.id.setText(getId(getItem(position)));
        holder.name.setText(getName(getItem(position)));
        holder.price.setText("£ " + getPrice(getItem(position)));
        holder.description.setText(getDesc(getItem(position)));
        String path = getImgPath(getItem(position));
        if(a==itemsList){

            final String quantity = "1";

            sign = "+";

            if(path.equals("") || path.equals(null)){
                //holder.img.setImageDrawable(defaul);
                holder.img.setContentDescription("default");
            }
            else{
                Bitmap bm = BitmapFactory.decodeFile(path);
                holder.img.setImageBitmap(bm);
                holder.img.setContentDescription("set");
            }


        holder.img.setOnClickListener(new View.OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(View v) {
                final ImageView full = (ImageView) findViewById(R.id.fullScreen);
                full.setVisibility(View.VISIBLE);
                full.setEnabled(true);

                full.setBackgroundColor(Color.BLACK);
                full.setImageDrawable(holder.img.getDrawable());

                full.setScaleType(ImageView.ScaleType.CENTER_INSIDE);


                full.setOnTouchListener(new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        full.setVisibility(View.GONE);
                        full.setEnabled(false);
                        return true;

                    }
                });

            }
        });
        holder.button.setOnClickListener(new android.view.View.OnClickListener(){
             public void onClick(View v) {
              //System.out.println("id" + v.getId());
              //System.out.println("bid" + R.id.order);
                // System.out.println("hell :" + lv.getItemAtPosition(position));
                 //System.out.println("hell :" + lv.getItemAtPosition(position));

              HashMap<String, String> map = itemsList.get(position);
            // System.out.println("items :" + lv.getAdapter().getItem(position));
             //System.out.println("orders :" + orderList.toString());

          if(v.getId() == R.id.order)
         {
              HashMap<String, String> hsh = map;

              String ss = null ;

              boolean done = false;
              for(int i=0; orderList.size()>i; i++){
                  HashMap<String, String> s = orderList.get(i);
                  ss= s.get(TAG_ID).toString();
                 if ( ss.equalsIgnoreCase(getItem(position).get(TAG_ID).toString()))
                  {
                      int foo = Integer.parseInt(s.get(TAG_QUANTITY).toString());
                      s.put(TAG_QUANTITY,( "" + (foo+1)));
                      done=true;
                      break;
                  }

              }
              if(!done)
              {
                  hsh.put(TAG_QUANTITY, quantity);
                  orderList.add(hsh);
              }


              //Log.d("Selected : ", orderList.toString());
            //  ListAdapter order = new MyCustomAdapter(1,orderList,R.layout.list_item);
              // updating listview
             // list1.setAdapter(order);
             // list11.setAdapter(order);

            ((MyCustomAdapter)  list1.getAdapter()).notifyDataSetChanged();
            ((MyCustomAdapter)  list11.getAdapter()).notifyDataSetChanged();

                      Float fo = Float.parseFloat(map.get(TAG_PRICE).toString());
                      Float total = Float.parseFloat(t.getText().toString());

                      //System.out.println("heeeeeeeeeeeeeere :" +fo);
                      t.setText(""+ String.format("%.2f", (fo+total)));
                      totalll = Float.parseFloat(t.getText().toString());
              }
        }
      });
        }
        else
        {                   
            sign = "-";
            if (value == 1)
            {
                holder.quantity.setVisibility(View.VISIBLE);
            }

            ListView l = list1;
            if(((Button) findViewById(R.id.handle)).isPressed())
            {
                l = list11;
            }else{l = list1;}
            final SwipeDetector swipeDetector = new SwipeDetector();
            l.setOnTouchListener(swipeDetector);
            l.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        if (swipeDetector.swipeDetected()){                                                               
                              Float fo = Float.parseFloat((getItem(position).get(TAG_PRICE)).toString());
                              Float Quant = Float.parseFloat((getItem(position).get(TAG_QUANTITY)).toString());
                              Float total = Float.parseFloat(t.getText().toString());
                              //System.out.println("heeeeeeeeeeeeeere :" +fo);
                              orderList.remove(getItem(position));
                              t.setText(""+ String.format("%.2f", (total-(fo*Quant))));
                    //remove        //  ListAdapter order = new MyCustomAdapter(1,orderList,R.layout.list_item);
                    //remove        //  list1.setAdapter(order);
                    //remove        //  list11.setAdapter(order);
                              ((MyCustomAdapter)  list1.getAdapter()).notifyDataSetChanged();
                              ((MyCustomAdapter)  list11.getAdapter()).notifyDataSetChanged();
                            } 


                     }
            });
            holder.button.setOnClickListener(new View.OnClickListener(){
                 public void onClick(View v) {
                 // System.out.println("id " + v.getId());
                 // System.out.println("bid " + R.id.order);
                  HashMap<String, String> map = getItem(position);


              if(v.getId() == R.id.order)
             {

                  if ( orderList.contains(getItem(position)) ){
                      System.out.println("item :" +getItem(position));
                      //holder.quantity.setText("q");
                      int foo = Integer.parseInt(map.get(TAG_QUANTITY).toString());

                      map.put(TAG_QUANTITY, "" + (foo-1));
                      if((foo-1)==0)
                      {
                          orderList.remove(getItem(position));
                      }

                  }

                 // ListAdapter order = new MyCustomAdapter(1,orderList,R.layout.list_item);
                          // updating listview
                //        list1.setAdapter(order);
                //        list11.setAdapter(order);
                  ((MyCustomAdapter)  list1.getAdapter()).notifyDataSetChanged();
                  ((MyCustomAdapter)  list11.getAdapter()).notifyDataSetChanged();

                          Float fo = Float.parseFloat(map.get(TAG_PRICE).toString());
                          Float total = Float.parseFloat(t.getText().toString());

                          //System.out.println("heeeeeeeeeeeeeere :" +fo);
                          t.setText(""+ String.format("%.2f", (total-fo)));
                  }
            }
          });
            holder.button.setOnLongClickListener(new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {
                      Float fo = Float.parseFloat((getItem(position).get(TAG_PRICE)).toString());
                      Float Quant = Float.parseFloat((getItem(position).get(TAG_QUANTITY)).toString());
                      Float total = Float.parseFloat(t.getText().toString());
                      //System.out.println("heeeeeeeeeeeeeere :" +fo);
                      orderList.remove(getItem(position));
                      t.setText(""+ String.format("%.2f", (total-(fo*Quant))));
                      ((MyCustomAdapter) list1.getAdapter()).notifyDataSetChanged();
                      ((MyCustomAdapter) list11.getAdapter()).notifyDataSetChanged();
                    return false;
                }
            });
        }


        if(a==orderList){holder.quantity.setText(getQuant(getItem(position)));}
        if(sign=="-"){holder.button.setText(sign);}
        return convertView;
    }

1 个答案:

答案 0 :(得分:0)

确保在持有人设置数据时涵盖所有信息案例,

我的意思是,如果第一项在数据行上没有“描述”,那么你向下滚动然后向上这个项目将从其他行设置“描述”。

只需在holder ==null&amp;当holder !=null时,检查每个项目是否有值,如果没有设置默认值。

我希望答案可以帮助你