2013-12-16 77 views
1

我經歷了很多關於如何分離項目並將它們分組在一個標題下的主題的文章。總之,我喜歡根據是否顯示標題來使標題可見和不可見。我也知道這個魔術發生在自定義適配器類的bindView/newView方法中。到現在爲止還挺好。現在我正在查詢數據庫中的日期,我希望屬於唯一日期的項目在日期標題下組合在一起。 (這是我想按日期分組項目)有沒有辦法我可以檢查日期是否有其他條件(從光標獲取的日期),並根據這些我將它們組合在一起。我發現的一篇這樣的文章是This。但是我無法完全理解它。對我來說,一個算法來做同樣的工作。將標題添加到ListView行或將標題附加到項目組

到目前爲止,這是我的代碼:

private class CurAdapter extends CursorAdapter{ 
     // Context context; 
     // Cursor c; 
     public CurAdapter(Context context, Cursor c, int flags) { 
      super(context, c, flags); 

      //  this.context = context; 
      //  this.c= c; 

     } 


     @Override 
     public void bindView(View view, Context context, Cursor c) { 
      TextView tV = (TextView)view.findViewById(R.id.textView1); 
      TextView tV1 = (TextView)view.findViewById(R.id.textView2); 
      TextView tV2 = (TextView)view.findViewById(R.id.textView3); 
      ImageView iM = (ImageView)view.findViewById(R.id.imageView2); 

      tV1.setTypeface(tf); 
      tV2.setTypeface(tf); 

      tV.setText(dateConvert(c.getString(c.getColumnIndexOrThrow("DateToNotify")))); 
      String B = c.getString(c.getColumnIndexOrThrow("NotificationFor")); 
      tV1.setText(c.getString(c.getColumnIndexOrThrow("NotificationDateFor"))); 



      if(B.equalsIgnoreCase("1")){ 
       tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX"); 
       iM.setBackgroundResource(R.drawable.icon_XXX); 


      }else if(B.equalsIgnoreCase("2")){ 
       tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX"); 
       iM.setBackgroundResource(R.drawable.icon_XXX); 


      }else if(B.equalsIgnoreCase("3")){ 
       tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX"); 
       iM.setBackgroundResource(R.drawable.icon_XXX); 


      }else if(B.equalsIgnoreCase("4")){ 
       tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX"); 
       iM.setBackgroundResource(R.drawable.icon_XXX); 


      }else if(B.equalsIgnoreCase("5")){ 
       tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX"); 
       iM.setBackgroundResource(R.drawable.icon_XXX); 


      }else if(B.equalsIgnoreCase("6")){ 
       tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX"); 
       iM.setBackgroundResource(R.drawable.icon_XXX); 


      }else if(B.equalsIgnoreCase("7")){ 
       tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX"); 
       iM.setBackgroundResource(R.drawable.icon_XXX); 

      } 


     } 

     @Override 
     public View newView(Context context, Cursor c, ViewGroup parent) { 


      LayoutInflater inflater = LayoutInflater.from(context); 
      View v = inflater.inflate(R.layout.detail_list, parent, false); 
      bindView(v, context, c); 
      return v; 
     } 

    } 

基本上我想一個方法來檢查什麼DateToNotify持有的每一行,取決於這個我會告訴隱藏的看法。任何提示?

編輯

到目前爲止,這是我已經盡力了,雖然我得到了想要的結果,但滾動列表弄亂了一切:

@Override 
     public void bindView(View view, Context context, Cursor c) { 

      TextView tV = (TextView)view.findViewById(R.id.textView1); 
      TextView tV1 = (TextView)view.findViewById(R.id.textView2); 
      TextView tV2 = (TextView)view.findViewById(R.id.textView3); 
      ImageView iM = (ImageView)view.findViewById(R.id.imageView2); 

      tV1.setTypeface(tf); 
      tV2.setTypeface(tf); 


      if(tV.getText().toString().equals(dateConvert(c.getString(c.getColumnIndexOrThrow("DateToNotify"))))){ 


       tV.setVisibility(View.INVISIBLE); 
      }else{ 
       tV.setVisibility(View.VISIBLE); 

       tV.setText(dateConvert(c.getString(c.getColumnIndexOrThrow("DateToNotify")))); 
      } 

編輯

優化代碼按照This Question on StackOverflow

@Override 
     public View newView(Context context, Cursor c, ViewGroup parent) { 


      View view = LayoutInflater.from(context).inflate(R.layout.detail_list, null); 
      //bindView(v, context, c); 
      TextView tV = (TextView)view.findViewById(R.id.textView1); 
      TextView tV1 = (TextView)view.findViewById(R.id.textView2); 
      TextView tV2 = (TextView)view.findViewById(R.id.textView3); 
      ImageView iM = (ImageView)view.findViewById(R.id.imageView2); 
      RelativeLayout RL = (RelativeLayout)view.findViewById(R.id.relative1); 

      view.setTag(R.id.textView1, tV); 
      view.setTag(R.id.textView2, tV1); 
      view.setTag(R.id.textView3, tV2); 
      view.setTag(R.id.imageView2, iM); 
      view.setTag(R.id.relative1, RL); 
      return view; 
     } 

而且BindView:

@Override 
     public void bindView(View view, Context context, Cursor c) { 

      ((TextView) view.getTag(R.id.textView2)).setTypeface(tf); 
      ((TextView) view.getTag(R.id.textView3)).setTypeface(tf); 
     // RelativeLayout RL =(RelativeLayout)view.findViewById(R.id.relative1); 
      String Bb = ((TextView) view.getTag(R.id.textView1)).getText().toString(); 
      if(Bb.equals(dateConvert(c.getString(c.getColumnIndexOrThrow("DateToNotify"))))){ 

       Toast.makeText(getActivity(), "If Condition: "+Bb, 10000).show(); 
       ((RelativeLayout)view.getTag(R.id.relative1)).setVisibility(View.INVISIBLE); 

      }else{ 

       ((RelativeLayout)view.getTag(R.id.relative1)).setVisibility(View.VISIBLE); 

       ((TextView) view.getTag(R.id.textView1)).setText(dateConvert(c.getString(c.getColumnIndexOrThrow("DateToNotify")))); 
      } 

回答

0

這裏是專爲您的問題的解決方案:(根據您所提供的鏈接)

private class CurAdapter extends CursorAdapter{ 


     private static final int VIEW_TYPE_GROUP_START = 0; 
     private static final int VIEW_TYPE_GROUP_CONT = 1; 
     private static final int VIEW_TYPE_COUNT = 2; 

     LayoutInflater mInflater ; 
     public CurAdapter(Context context, Cursor c, int flags) { 
      super(context, c, flags); 
      mInflater = LayoutInflater.from(context); 


     } 


     @Override 
     public int getViewTypeCount() { 
      return VIEW_TYPE_COUNT; 
     } 

     @Override 
     public int getItemViewType(int position) { 
      if (position == 0) { 
       return VIEW_TYPE_GROUP_START; 
      } 
      Cursor cursor = getCursor(); 
      cursor.moveToPosition(position); 
      boolean newGroup = isNewGroup(cursor, position); 

      if (newGroup) { 
       return VIEW_TYPE_GROUP_START; 
      } else { 
       return VIEW_TYPE_GROUP_CONT; 
      } 
     } 

     @Override 
     public void bindView(View view, Context context, Cursor c) { 

      TextView tV = (TextView)view.findViewById(R.id.textView1); 
      TextView tV1 = (TextView)view.findViewById(R.id.textView2); 
      TextView tV2 = (TextView)view.findViewById(R.id.textView3); 
      ImageView iM = (ImageView)view.findViewById(R.id.imageView2); 
      tV1.setTypeface(tf); 
      tV2.setTypeface(tf); 
      if(tV!=null) 
       tV.setText(dateConvert(c.getString(c.getColumnIndexOrThrow("DateToNotify")))); 


      String B = c.getString(c.getColumnIndexOrThrow("NotificationFor")); 
      tV1.setText(c.getString(c.getColumnIndexOrThrow("NotificationDateFor"))); 


      if(B.equalsIgnoreCase("1")){ 
       tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX"); 
       iM.setBackgroundResource(R.drawable.XXX); 

      }else if(B.equalsIgnoreCase("2")){ 
       tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX"); 
       iM.setBackgroundResource(R.drawable.icon_XXX); 

      }else if(B.equalsIgnoreCase("3")){ 

       tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX"); 

       iM.setBackgroundResource(R.drawable.icon_XXX); 

      }else if(B.equalsIgnoreCase("4")){ 

       tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX"); 
       iM.setBackgroundResource(R.drawable.icon_XXX); 

      }else if(B.equalsIgnoreCase("5")){ 

       tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX"); 
       iM.setBackgroundResource(R.drawable.icon_XXX); 

      }else if(B.equalsIgnoreCase("6")){ 
       tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX"); 
       iM.setBackgroundResource(R.drawable.icon_XXX); 

      }else if(B.equalsIgnoreCase("7")){ 

       tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX"); 
       iM.setBackgroundResource(R.drawable.icon_XXX); 
      } 
     } 

     @Override 
     public View newView(Context context, Cursor cursor, ViewGroup parent) { 

      int position = cursor.getPosition(); 
      int nViewType; 

      if (position == 0) { 
       nViewType = VIEW_TYPE_GROUP_START; 
      } else { 
       boolean newGroup = isNewGroup(cursor, position); 
       if (newGroup) { 
        nViewType = VIEW_TYPE_GROUP_START; 
       } else { 
        nViewType = VIEW_TYPE_GROUP_CONT; 
       } 
      } 

      View v; 

      if (nViewType == VIEW_TYPE_GROUP_START) { 
       v = mInflater.inflate(R.layout.detail_list, parent, false); 

       View vHeader = v.findViewById(R.id.relativeSecondary); 
       vHeader.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View v) { 


        } 
       }); 
      } else { 

       v = mInflater.inflate(R.layout.detail_list_no_header, parent, false); 
      } 
      return v; 
     } 


     private boolean isNewGroup(Cursor cursor, int position) { 
      String ThisDate = dateConvert(cursor.getString(cursor.getColumnIndexOrThrow("DateToNotify"))); 
      cursor.moveToPosition(position - 1); 
      String compareDate = dateConvert(cursor.getString(cursor.getColumnIndexOrThrow("DateToNotify"))); 
      cursor.moveToPosition(position);  
      if (!ThisDate.equalsIgnoreCase(compareDate)) { 
       return true; 
      } 

      return false; 
     } 
    } 
+0

功效神奇! – User3