【问题标题】:Group returned records by date in Firebase在 Firebase 中按日期对返回的记录进行分组
【发布时间】:2018-04-03 15:59:27
【问题描述】:

我有一个 Android 应用,它从 Firebase 数据库返回 ListView 的足球赛程。该列表目前有效,但我想改进 UI。下图显示了我所拥有的以及我想要实现的目标;

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

下面是我的xml的布局;

为了让您了解代码(请询问您是否想看代码,我最近发布了 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" />

日期红色标题在列表视图 xml 中。

我猜这是一个递归函数,它计算出一个日期的所有孩子,然后绘制出列表。

我的问题是,您会推荐哪种方法,因为我不想降低性能,而且我在 2-3 种方法之间纠结。

编辑: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;
  }
}

【问题讨论】:

  • 请分享您将数据从数据库设置到 ListView/RecyclerView 的代码。
  • @AlexMamo 刚刚用适配器代码更新了问题

标签: java android firebase firebase-realtime-database


【解决方案1】:

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

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

【讨论】:

  • dateofmatch 是定义整个列表的布局的一部分,虽然我确实看到了上面代码的发展方向,但我会考虑这个想法
猜你喜欢
  • 2014-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-23
  • 1970-01-01
  • 2011-04-08
  • 2014-01-31
  • 1970-01-01
相关资源
最近更新 更多