2012-07-04 46 views
0

我想使用Custom BaseAdapter創建自定義ListView,其中status = 1,我想顯示CheckBox,否則我想顯示textView .. 如何在自定義BaseAdapter的getView中創建條件


我給出的條件是:

if (NewtheStatus == 1) { 
        alreadyOrderText 
        .setVisibility(TextView.GONE); 

       } 
       else{ 
        checkBox.setVisibility(CheckBox.GONE); 

       } 

但有些時候我獲得一些既沒有checkBox也沒有TextView的行。


我的自定義BaseAdapter的代碼如下所示。

private class MyCustomAdapter extends BaseAdapter { 

    private static final int TYPE_ITEM = 0; 
    private static final int TYPE_ITEM_WITH_HEADER = 1; 
    // private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1; 

    private ArrayList<WatchListAllEntity> mData = new ArrayList(); 
    private LayoutInflater mInflater; 
    private ArrayList<WatchListAllEntity> items = new ArrayList<WatchListAllEntity>(); 

    public MyCustomAdapter(Context context, 
      ArrayList<WatchListAllEntity> items) { 

     mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public void addItem(WatchListAllEntity watchListAllEntity) { 
     // TODO Auto-generated method stub 
     items.add(watchListAllEntity); 
    } 

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

     View v = convertView; 
     final int position1 = position; 
     if (v == null) { 

      LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.listitempict, null); 
     } 
     watchListAllEntity = new WatchListAllEntity(); 
     watchListAllEntity = items.get(position); 
     Log.i("position: iteamsLength ", position + ", " + items.size()); 
     if (watchListAllEntity != null) { 

      ImageView itemImage = (ImageView) v 
        .findViewById(R.id.imageviewproduct); 

      if (watchListAllEntity.get_thumbnail_image_url1() != null) { 
       Drawable image = ImageOperations(watchListAllEntity 
         .get_thumbnail_image_url1().replace(" ", "%20"), 
         "image.jpg"); 

       // itemImage.setImageResource(R.drawable.icon); 

       if (image != null) { 
        itemImage.setImageDrawable(image); 
        itemImage.setAdjustViewBounds(true); 
       } else { 

        itemImage.setImageResource(R.drawable.iconnamecard); 
       } 

       Log.i("status_ja , status", 
         watchListAllEntity.get_status_ja() + " ," 
           + watchListAllEntity.getStatus()); 

       int NewtheStatus = Integer.parseInt(watchListAllEntity 
         .getStatus()); 
       CheckBox checkBox = (CheckBox) v 
         .findViewById(R.id.checkboxproduct); 
       TextView alreadyOrderText = (TextView) v 
         .findViewById(R.id.alreadyordertext); 
       if (NewtheStatus == 1) { 
        alreadyOrderText 
        .setVisibility(TextView.GONE); 

       } 
       else{ 
        checkBox.setVisibility(CheckBox.GONE); 

       } 
       Log.i("Loading ProccardId: ", 
         watchListAllEntity.get_proc_card_id() + ""); 
       checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

        public void onCheckedChanged(CompoundButton buttonView, 
          boolean isChecked) { 
         WatchListAllEntity watchListAllEntity2 = items 
           .get(position1); 
         Log.i("Position: ", position1 + ""); 
         // TODO Auto-generated method stub 
         if (isChecked) { 


           Constants.orderList.add(watchListAllEntity2 
             .get_proc_card_id()); 
           Log.i("Proc Card Id Add: ", 
             watchListAllEntity2 
               .get_proc_card_id() + ""); 


         } else { 
          Constants.orderList.remove(watchListAllEntity2 
            .get_proc_card_id()); 
          Log.i("Proc Card Id Remove: ", 
            watchListAllEntity2.get_proc_card_id() 
              + ""); 

         } 

        } 
       }); 
      } 

     } 

     return v; 
    } 

    private Drawable ImageOperations(String url, String saveFilename) { 
     try { 
      InputStream is = (InputStream) this.fetch(url); 
      Drawable d = Drawable.createFromStream(is, "src"); 

      return d; 
     } catch (MalformedURLException e) { 
      return null; 
     } catch (IOException e) { 
      return null; 
     } 
    } 

    public Object fetch(String address) throws MalformedURLException, 
      IOException { 
     URL url = new URL(address); 
     Object content = url.getContent(); 
     return content; 
    } 

    public int getCount() { 
     // TODO Auto-generated method stub 
     int sizef=items.size(); 
     Log.i("Size", sizef+""); 
     return items.size(); 
    } 

    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return items.get(position); 
    } 

    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

} 
+0

所以你知道哪一行沒有得到TextView或CheckBox。現在檢查該位置的logcat什麼是我檢查過的NewtheStatus –

+0

的值。在調試代碼的情況下工作正常,但是,當調試完成時,輸出是錯誤的.. – TKumar

回答

1

我創建了一個錯誤... 裏面我的代碼,我犯了這樣的

if (NewtheStatus == 1) { 
    alreadyOrderText.setVisibility(TextView.GONE); 
} else { 
    checkBox.setVisibility(CheckBox.GONE); 
} 

正確的代碼一個條件是:

if (NewtheStatus == 1) { 
    alreadyOrderText.setVisibility(TextView.INVISIBLE); 
    checkBox.setVisibility(CheckBox.VISIBLE); 
} else { 
    checkBox.setVisibility(CheckBox.INVISIBLE); 
    alreadyOrderText.setVisibility(TextView.VISIBLE); 
} 

和我的XML裏面,我宣佈CheckBox和的TextView那些是不可見的。現在代碼運行良好。

3

當您滾動時,ListView將重用行的佈局。如果您在某個時間隱藏了TextViewCheckBox,並且Android重新使用該行,則View將保持隱藏狀態。

您需要做的就是在getView()方法中爲要顯示的每個View設置View.VISIBLE的可見性。

相關問題