2013-12-16 6 views
1

Я просмотрел множество статей по этому вопросу о том, как разделять элементы и группировать их под одним заголовком. Из всего, что мне понравилась идея сделать заголовок видимым и невидимым в зависимости от того, показывать это или нет. Я также знаю, что эта магия происходит в методе bindView/newView класса адаптивного адаптера. Все идет нормально. Теперь я запрашиваю базу данных для дат, я хочу, чтобы элементы, подпадающие под уникальную дату, группировались вместе под заголовком даты. (То есть, я хочу группировать элементы по дате). Есть ли способ проверить даты в if else условиях (Даты, полученные от курсора), и в зависимости от них я группирую их вместе. Одна такая статья на эту тему я нашел 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 для каждой строки, в зависимости от этого я покажу Хид вида. Любые подсказки?

EDIT

До сих пор это то, что я пытался, хотя я получить желаемый результат, но прокрутки списка путает все:

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

EDIT

Оптимизированный код в соответствии с 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

Вот решение настроить для вашей проблемы: (На основании Link вы предоставили)

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

работает как Magic! – User3

Смежные вопросы