美好的一天
我搜索了网站,但在我的问题上无法得到任何帮助。 我是android的新手,开始了一个我无法完成的项目。
使用Android Studio 2.3 应用名称“匹配跟踪器”
好的,这就是了 例如我的应用程序允许您跟踪匹配“橄榄球”现在我希望该应用程序自动添加积分到排行榜,如果一个团队有4次或更多次尝试,这就是我被卡住的地方!!
我的FactDBAdaptor.class
EDITED *******
package za.co.sanrl.rugbyleague.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import za.co.sanrl.rugbyleague.main.MatchEvent;
import java.util.ArrayList;
/**
* Class that allows interaction with the Facts database such as insert, delete, update.
*
* @author Morne van Rooyen
*/
public class FactsDbAdapter {
public static final String KEY_MATCHID = "match_id";
public static final String KEY_TYPE = "type";
public static final String KEY_PLAYER1 = "player1";
public static final String KEY_PLAYER2 = "player2";
public static final String KEY_TEAM = "team";
public static final String KEY_TIME = "time";
private static final String DB_TABLE = "Facts";
private SQLiteDatabase db;
private BaseHelper dbHelper;
private Context context;
public FactsDbAdapter(Context context) {
this.context = context;
}
/**
* Open database so we can write to it.
*
* @return Open DB connection
* @throws SQLException
*/
public FactsDbAdapter open() throws SQLException {
dbHelper = new BaseHelper(context);
db = dbHelper.getWritableDatabase();
return this;
}
/**
* Close database.
*/
public void close() {
dbHelper.close();
}
/**
* Add a match fact to the database.
*
* @param match_id Match ID
* @param type Goal, penalty etc.
* @param player1 First player involved in event
* @param player2 Second player involved in event
* @param team The player(s) team
* @param time The time event took place
*/
public void addMatchFact(int match_id, String type, String player1, String player2, String team, String time) {
ContentValues values = new ContentValues();
values.put(KEY_MATCHID, match_id);
values.put(KEY_TYPE, type);
values.put(KEY_PLAYER1, player1);
values.put(KEY_PLAYER2, player2);
values.put(KEY_TEAM, team);
values.put(KEY_TIME, time);
//Insert into database.
try {
db.insert(DB_TABLE, null, values);
} catch (Exception e) {
Log.e("Database error when inserting match fact", e.toString());
e.printStackTrace();
}
}
/**
* Get the column values for a specific column provided.
*
* @param column - column you want to query.
* @param duplicates - if you want duplicate results.
* @return columnlist
*/
public ArrayList<String> getColumnValues(String column, boolean duplicates) {
Cursor cursor = null;
ArrayList<String> columnList = new ArrayList<>();
//Get column values.
try {
if (duplicates) {
cursor = db.rawQuery("SELECT " + column + " FROM " + DB_TABLE, null);
} else {
cursor = db.rawQuery("SELECT DISTINCT " + column + " FROM " + DB_TABLE, null);
}
} catch (Exception e) {
Log.e("Database error selecting facts", e.toString());
e.printStackTrace();
}
//Query result is not empty.
if (cursor != null && cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(column);
do {
columnList.add(cursor.getString(columnIndex));
} while (cursor.moveToNext());
}
return columnList;
}
/**
* Count number of types with a given match_id.
*
* @param match_id Match ID
* @param type Event Type
* @param teamName Team Name
* @return count of types
*/
public int countTypesWithMatchID(int match_id, String type, String teamName) {
Cursor cursor = null;
ArrayList<String> columnList = new ArrayList<>();
//Get column values.
try {
cursor = db.rawQuery("SELECT " + KEY_TYPE + " FROM " + DB_TABLE +
" WHERE " + KEY_TYPE + "=? AND " + KEY_MATCHID + "=" + match_id +
" AND " + KEY_TEAM + "=?", new String[]{type, teamName});
} catch (Exception e) {
Log.e("Database error counting match events", e.toString());
e.printStackTrace();
}
//Query result is not empty.
if (cursor != null && cursor.moveToFirst()) {
int typeIndex = cursor.getColumnIndex(KEY_TYPE);
do {
columnList.add(cursor.getString(typeIndex));
} while (cursor.moveToNext());
}
return columnList.size();
}
/**Count 4 or more tries in a match begins**/
/**
* Count number of types with a given match_id.
*
* @param match_id Match ID
* @param type type is Tries
* @param teamName Team Name
* @return count of types
*/
public int countTriesWithMatchID(int match_id, String type, String teamName) {
Cursor cursor = null;
int result = 0;
//Get column values.
try {
cursor = db.rawQuery("SELECT COUNT(" + KEY_TYPE + ") FROM " + DB_TABLE +
" WHERE " + KEY_TYPE + "=Try AND " + KEY_MATCHID + "=" + match_id +
" AND " + KEY_TEAM + "=?", new String[]{type, teamName});
} catch (Exception e) {
Log.e("Database error counting match events", e.toString());
e.printStackTrace();
}
//Query result is not empty.
if (cursor != null && cursor.moveToFirst()) {
result = cursor.getInt(0);
}
return result;
}
/**Count 4 or more tries in a match ends**/
/**
* Count the number a certain type with a certain player name comes up in db.
*
* @param type Event Type
* @param player Player involved in event
* @return count of types involving a specific player
*/
public int countTypesWithPlayer(String type, String player) {
Cursor cursor = null;
int result = 0;
//Get column values.
try {
cursor = db.rawQuery("SELECT COUNT(" + KEY_TYPE + ") FROM " + DB_TABLE +
" WHERE " + KEY_TYPE + "=? AND " + KEY_PLAYER1 + "=?", new String[]{type, player});
} catch (Exception e) {
Log.e("Db err counting type of events involving a player", e.toString());
e.printStackTrace();
}
//Query result is not empty.
if (cursor != null && cursor.moveToFirst()) {
result = cursor.getInt(0);
}
return result;
}
/**
* Get all match facts for a given match id.
*
* @param match_id Match ID
* @return All match facts
*/
public ArrayList<MatchEvent> getAllFactsForGivenMatchID(int match_id) {
Cursor cursor = null;
ArrayList<MatchEvent> result = new ArrayList<>();
//Get column values.
try {
cursor = db.rawQuery("SELECT * FROM " + DB_TABLE + " WHERE match_id=" + match_id, null);
} catch (Exception e) {
Log.e("Db err select * events", e.toString());
e.printStackTrace();
}
//Query result is not empty.
if (cursor != null && cursor.moveToFirst()) {
int typeIndex = cursor.getColumnIndex(FactsDbAdapter.KEY_TYPE);
int player1Index = cursor.getColumnIndex(FactsDbAdapter.KEY_PLAYER1);
int player2Index = cursor.getColumnIndex(FactsDbAdapter.KEY_PLAYER2);
int teamIndex = cursor.getColumnIndex(FactsDbAdapter.KEY_TEAM);
int timeIndex = cursor.getColumnIndex(FactsDbAdapter.KEY_TIME);
do {
String type = cursor.getString(typeIndex);
String player1 = cursor.getString(player1Index);
String player2 = cursor.getString(player2Index);
String team = cursor.getString(teamIndex);
String time = cursor.getString(timeIndex);
if (player2 != null) {
MatchEvent matchEvent = new MatchEvent(time, type, player1, player2, team);
result.add(matchEvent);
} else {
MatchEvent matchEvent = new MatchEvent(time, type, player1, team);
result.add(matchEvent);
}
} while (cursor.moveToNext());
}
return result;
}
}
现在在我的RugbyActivity.class中,无效的部分就是这个
int bpts = factsDbAdapter.countTriesWithMatchID(factsDbAdapter.KEY_MATCHID, factsDbAdapter.KEY_TYPE, factsDbAdapter.KEY_TEAM);
if (bpts<3)
任何人都可以帮助我或引导我朝正确的方向发展,请更好地描述下面的代码
switch (status) {
case WIN: {// Team won the match.
int totalWins = teamAdapter.getColumnValueForTeamInt(teamAdapter.KEY_WINS, team);
int totalPoints = teamAdapter.getColumnValueForTeamInt(teamAdapter.KEY_TOTALPOINTS, team);
int totalBonusPoints = teamAdapter.getColumnValueForTeamInt(teamAdapter.KEY_BONUSPOINTS, team);
teamAdapter.updateSingleColumn(team, teamAdapter.KEY_WINS, totalWins + 1);
int bpts = factsDbAdapter.countTriesWithMatchID(factsDbAdapter.KEY_MATCHID, factsDbAdapter.KEY_TYPE, factsDbAdapter.KEY_TEAM);
if (bpts<3) {
teamAdapter.updateSingleColumn(team, teamAdapter.KEY_BONUSPOINTS, totalBonusPoints + 1);
teamAdapter.updateSingleColumn(team, teamAdapter.KEY_TOTALPOINTS, totalPoints + 5);
}
else
{
teamAdapter.updateSingleColumn(team, teamAdapter.KEY_TOTALPOINTS, totalPoints + 4);
}
break;
}
提前谢谢
答案 0 :(得分:1)
您的方法定义为:
public int countTriesWithMatchID(int match_id, String type, String teamName)
你用它来称呼它:
factsDbAdapter.countTriesWithMatchID(factsDbAdapter.KEY_MATCHID, factsDbAdapter.KEY_TYPE, factsDbAdapter.KEY_TEAM)
如果替换正在使用的常量,则呼叫变为:
factsDbAdapter.countTriesWithMatchID("match_id", "type", "team")
但是,这不起作用,因为它们都是String
类型,而您的countTriesWithMatchID
需要int
,String
和另一个String
。
所以,问题是你需要一个int
作为第一个参数,即匹配的id。像这样的电话会起作用,但你需要正确的身份证号码:
factsDbAdapter.countTriesWithMatchID(1, "type", "team")
我希望这会有所帮助。