在自定义列表视图的适配器中隐藏文本视图,以获得循环

时间:2018-01-24 11:28:10

标签: android listview android-adapter

我有一个自定义列表视图,其中包含一个包含日期,职责和名称的自定义行。如果日期是两次,则应隐藏第二个日期。如何从适配器隐藏特定位置的日期。

我的程序看起来像这样

这是自定义行。

        <TextView
            android:id="@+id/tv_duty_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
       <TextView
            android:id="@+id/tv_duty_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
       <TextView
                android:id="@+id/tv_duty_member_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

这是我的适配器

TextView name, duty, date_time;

viewHolder.duty = (TextView) convertView.findViewById(R.id.tv_duty_name);

viewHolder.name = (TextView)convertView.findViewById(R.id.tv_duty_member_name);

viewHolder.date_time = (TextView)convertView.findViewById(R.id.tv_duty_date);

for (int j = 0; j < data.size(); j++){
            if (j == 0){

            }else {
                String current_date = data.get(j).getDateTime());
                String previous_previous = data(j-1).getDateTime());
                if (current_date.equals(previous_date)) {
                    viewHolder.date_time.setVisibility(View.INVISIBLE);
                }
            }
        }

如果我这样做,所有日期都会被隐藏。我想在两个日期相等时隐藏。

4 个答案:

答案 0 :(得分:1)

添加其他部分

for (int j = 0; j < data.size(); j++){
        if (j == 0){

        }else {
            String current_date = data.get(j).getDateTime());
            String previous_previous = data(j-1).getDateTime());
            if (current_date.equals(previous_date)) {
                viewHolder.date_time.setVisibility(View.INVISIBLE);
            }else{
                viewHolder.date_time.setVisibility(View.VISIBLE);
                }
        }
    }

答案 1 :(得分:0)

在getview()方法中尝试此代码,

if(position>0)
      {
           String current_date = data.get(position).getDateTime());
                String previous_previous = data(position-1).getDateTime());
                if (current_date.equals(previous_date)) {
                    viewHolder.date_time.setVisibility(View.GONE);
                }
                else  {
                     viewHolder.date_time.setVisibility(View.VISIBLE);
                }
      }

答案 2 :(得分:0)

试试这个

for (int j = 0; j < data.size(); j++){
            if (j == 0){

            }else {
                String current_date = data.get(j).getDateTime());
                String previous_previous = data(j-1).getDateTime());
                if (current_date.equals(previous_date)) {
                    viewHolder.date_time.setVisibility(View.INVISIBLE);
                }
                else
                {                        
                   viewHolder.date_time.setVisibility(View.VISIBLE);
                }
            }
        }

修改

if (current_date.equals(previous_date)) {
                        viewHolder.date_time.setVisibility(View.GONE);
                    }
                    else
                    {                        
                       viewHolder.date_time.setVisibility(View.VISIBLE);
                    }

答案 3 :(得分:0)

I will suggest check First which layout you want to show, then inflate the layout every time after checking inside "getView" function of Adapter.

View v = convertView;
  int type = getItemViewType(position);
  if (v == null) {
    // Inflate the layout according to the view type
   LayoutInflater inflater = (LayoutInflater) `enter code here`ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   if (type == 0) {
     // Inflate the layout with image
     v = inflater.inflate(R.layout.layout_with_date, parent, false);
  }
  else {
    v = inflater.inflate(R.layout.layout_with_out_date, parent, false);
  }
 }