【问题标题】:Adding a header to a ListView row or appending a header to group of items将标题添加到 ListView 行或将标题附加到项目组
【发布时间】:2013-12-16 07:17:58
【问题描述】:

我浏览了很多关于如何分隔项目并将它们分组到一个标题下的主题的文章。其中,我喜欢根据是否显示来使标题可见和不可见的想法。我也知道这个魔法发生在自定义适配器类的 bindView/newView 方法中。到现在为止还挺好。现在我正在查询数据库中的日期,我希望将属于唯一日期的项目组合在一个日期标题下。 (也就是说,我想按日期对项目进行分组)有没有办法可以在 if else 条件(从光标中获取的日期)中检查日期,并根据这些条件将它们组合在一起。我发现的一篇关于这个主题的文章是This。但是我无法完全理解它。对我来说,做同样的算法会起作用。

到目前为止,这是我的代码:

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

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    这是为您的问题定制的解决方案:(基于您提供的链接)

    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;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2018-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-21
      • 2012-08-26
      • 1970-01-01
      相关资源
      最近更新 更多