gpt4 book ai didi

java - 获取投球手计数以显示在 League RecyclerView 中

转载 作者:行者123 更新时间:2023-12-02 09:39:00 25 4
gpt4 key购买 nike

我有一个联赛列表,我想在每个条目中显示投球手的数量。例如:

enter image description here

我想在列表中每个联盟名称下的每个列表中显示投球手的数量。例如:

enter image description here

这旨在快速浏览每个联赛。

我尝试使用以下代码来完成此操作:

数据库助手

//Getting Number of Bowlers in League
public String leagueBowlerCount(String leagueId)
{

SQLiteDatabase db = this.getReadableDatabase();
String countQuery = "SELECT * FROM " + Bowler.TABLE_NAME + " WHERE " + Bowler.COLUMN_LEAGUE_ID + " = '" + leagueId + "'";
Cursor cursor = db.rawQuery(countQuery, null);

int count = 0;
if(null != cursor)
if(cursor.getCount() > 0){
cursor.moveToFirst();
count = cursor.getInt(0);
}
cursor.close();


db.close();
return String.valueOf(count);
}

联赛适配器

public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView id;
public TextView name;
public TextView baseScore;
public TextView basePercentage;
public TextView bowlerCount;
TextView timestamp;
public TextView buttonViewOption;

MyViewHolder(View view) {
super(view);
if (!(itemView instanceof AdView)) {
id = view.findViewById( R.id.tvLeagueId);
name = view.findViewById(R.id.tvLeagueName );
baseScore = view.findViewById( R.id.tvBaseScore);
basePercentage = view.findViewById(R.id.tvBaseScorePercentage);
bowlerCount = view.findViewById(R.id.tvNumberOfBowlers);
timestamp = view.findViewById(R.id.timestamp);
buttonViewOption = view.findViewById(R.id.buttonViewOptions);
}
}
}

public LeagueAdapter(Context context, List<League> leaguesList) {
this.context = context;
this.leaguesList = leaguesList;
mainActivity = (Activity) context;
inflater = LayoutInflater.from(context);
}

public LeagueAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
AdView adview;
MyViewHolder holder;

if (viewType == AD_TYPE) {
adview = new AdView(mainActivity);
adview.setAdSize( AdSize.BANNER);

// this is the good adview
adview.setAdUnitId(mainActivity.getString(R.string.admob_ad_id));

float density = mainActivity.getResources().getDisplayMetrics().density;
int height = Math.round(AdSize.BANNER.getHeight() * density);
AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, height);
adview.setLayoutParams(params);

// dont use below if testing on a device
// follow https://developers.google.com/admob/android/quick-start?hl=en to setup testing device
AdRequest request = new AdRequest.Builder().build();
adview.loadAd(request);
holder = new MyViewHolder(adview);

}else{
View view = inflater.inflate(R.layout.listview_league, parent, false);
holder = new MyViewHolder(view);
}
return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
if(position % 10 != 5) {
League league = leaguesList.get(position);
int id = league.getId();
String leagueId = String.valueOf(id);
holder.id.setText(leagueId);
holder.name.setText(league.getName());
holder.baseScore.setText(league.getBasisScore());
holder.basePercentage.setText(league.getBaseScorePercentage());
holder.bowlerCount.setText(db.leagueBowlerCount(leagueId));
holder.timestamp.setText(formatDate(league.getTimestamp()));

holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//creating a popup menu
PopupMenu popup = new PopupMenu(context, holder.buttonViewOption);
//inflating menu from xml resource
popup.inflate(R.menu.league_options_menu);
//adding click listener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.profile:
//Log.d("leagueId", String.valueOf(position));
//int leagueId = league.getId();
((MainActivity) context).openDialog(true, leaguesList.get(position), position);
break;
case R.id.delete:
((MainActivity) context).deleteLeague(position);
break;
}
return false;
}
});
//displaying the popup
popup.show();
}
});
}
}

我已经搞乱这个好几天了,我不明白为什么这行不通。任何帮助将不胜感激。我认为可能有一种我不知道的更简单的方法来完成此任务。

在 logcat 中我看到以下消息:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String ca.vogl.r.tenpinbowlingcompanion.database.DatabaseHelper.leagueBowlerCount(java.lang.String)' on a null object reference.

正如下面所指出的,错误似乎发生在上面列出的 leagueBowlerCount() 中。

在 onBindViewHolder 中添加以下内容后:db = new DatabaseHelper (mainActivity)。我看到了我应该看到的值(value)观,但它们不正确。请参阅下面的图片。

enter image description here

测试联盟 1(有三名投球手,其中一名被测试广告隐藏)

enter image description here

测试联赛 2(只有 1 名投球手)

enter image description here

测试联赛 3(共有三名投球手,其中一名被测试广告隐藏)

enter image description here

所以基本上您应该看到测试联赛 1 为 3,测试联赛 2 为 1,测试联赛 3 为 3

所以现在看来​​问题出在我编写的 leagueBowlercount 函数上。它没有获取仅与个人联赛 ID 相关联的计数

最佳答案

我认为您的问题是您返回的是第一个选定的投球手的 ID,而不是行数。

也就是说,在检查行数大于 0 后,移动到第一行,然后使用 count =cursor.getInt(0); 这将是存储在已提取的第一行的第一列。

尝试使用:-

public String leagueBowlerCount(String leagueId)
{
String rv = "0";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(Bowler.TABLE_NAME,new String[]{"count(*)"},Bowler.COLUMN_LEAGUE_ID + "=?",new String[]{leagueId},null,null,null);
if (cursor.moveToFirst()) {
rv = String.valueOf(cursor.getLong(0));
}
cursor.close();
db.close();
return rv;
}

这使用聚合函数count来提取相应联赛的行数。

  • 请注意,上述代码是原则代码,尚未经过测试或运行,因此可能存在一些错误。

或者你可以使用:-

public String leagueBowlerCount(String leagueId)
{

SQLiteDatabase db = this.getReadableDatabase();
String countQuery = "SELECT * FROM " + Bowler.TABLE_NAME + " WHERE " + Bowler.COLUMN_LEAGUE_ID + " = '" + leagueId + "'";
Cursor cursor = db.rawQuery(countQuery, null);

int count = cursor.getCount();
cursor.close();
db.close();
return String.valueOf(count);
}

关于您的代码:-

int count = 0;
if(null != cursor)
if(cursor.getCount() > 0){
cursor.moveToFirst();
count = cursor.getInt(0);
}
cursor.close();
  • 检查 null Cursor 是没有用的,因为从 SQLiteDatabase 方法(例如 rawQuery)返回的 Cursor 永远不会为 null。相反,将返回一个有效的(可能是空的)游标。

  • 另外,不需要使用 getCount 方法检查 Cursor 是否有行,然后使用 moveToFirst,因为只需使用 if (cursor.moveToFirst) {.....} 就足够了,就好像没有行一样,moveToFirst 方法将返回 false,因为无法执行移动操作。

关于java - 获取投球手计数以显示在 League RecyclerView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57262029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com