分组在Firebase中按日期返回记录

时间:2018-04-02 11:56:15

标签: java android firebase firebase-realtime-database

我有一个Android应用,可以从Firebase数据库中返回ListView个足球设备。该列表目前有效,但我想改进用户界面。下图显示了我拥有的和我想要实现的目标;

enter image description here

在左侧我有一个布局xml循环记录并显示每条记录的日期,但我想要做的是返回一个列表,如果它们相同则显示1个日期的灯具,如果没有则显示在新的日期(如在右侧)。

下面是我的xml的布局;

enter image description here

为了让您了解代码(请询问您是否希望查看代码,我最近在过去发布了50行时被标记了);

<TextView
    android:id="@+id/txtDate"
    android:layout_width="0dp"
    android:layout_height="24dp"
    android:background="#ff0000"
    android:gravity="center_vertical"
    android:paddingLeft="5dp"
    android:textColor="@color/cardview_light_background"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="dateofmatch" />

日期红色标题位于listview xml中。

我猜这是一个递归函数,可以解决一个日期的所有子项,然后绘制出列表。

我的问题是,你会推荐哪种方法,因为我不想扼杀表现,而且我在两种方式之间挣扎。

编辑:ListView的适配器

public class matchList extends ArrayAdapter<matches> {
  private Activity context;
  private List<matches> matchList;

  public matchList(Activity context, List<matches> matcheslist) {
    super(context, R.layout.match_layout, matcheslist);
    this.context = context;
    this.matchList = matcheslist;
  }

 @NonNull
 @Override
 public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View listViewItem = inflater.inflate(R.layout.match_layout, null, true);
    TextView textViewDate = (TextView) listViewItem.findViewById(R.id.txtDate);
    TextView textGameID = (TextView) listViewItem.findViewById(R.id.txtGameID);
    TextView textViewTime = (TextView) listViewItem.findViewById(R.id.txtTime);
    TextView textViewHomeTeam = (TextView) listViewItem.findViewById(R.id.txtAction);
    TextView textViewAwayTeam = (TextView) listViewItem.findViewById(R.id.txtAwayTeam);
    TextView textViewPitchNo = (TextView) listViewItem.findViewById(R.id.txtPitch);
    ImageView imgLiveIco = (ImageView) listViewItem.findViewById(R.id.imgLive);

    matches match = matchList.get(position);
    textViewDate.setText(match.getDate());
    textGameID.setText(match.getGameID());
    textViewHomeTeam.setText(match.getHomeTeam());
    textViewAwayTeam.setText(match.getAwayTeam());
    textViewTime.setText(match.getTime());
    String fdate = helper.dateFormater(match.getDate() , "EE dd MMM yyyy" , "dd/MM/yyyy");
    textViewDate.setText(fdate);
    textViewPitchNo.setText("Pitch " + match.getPitch().toString());

    if(match.getPlayed() == 1) {
        imgLiveIco.setVisibility(View.VISIBLE);
    } else {
        imgLiveIco.setVisibility(View.INVISIBLE);
    }
    return listViewItem;
  }
}

1 个答案:

答案 0 :(得分:1)

为了达到你想要的效果,我建议你使用简单的if语句。我的代码中我不明白的事实是dateofmatch是整个项目视图的一部分。如果它是该视图的一部分,那么您需要使用findViewById()方法找到该视图并使用if语句,如下所示:

if (dateOfPreviousMatch.equals(dateOfMath)) {
    //set visibility of dateofmatch to GONE
} else {
    //set visibility of dateofmatch to VISIBLE
}
相关问题