我正在制作一个用于获取篮球统计数据的应用程序,但我遇到了一个小问题并且无法解决它。我遇到麻烦的部分是当你向团队添加一个玩家时,它应该将所有信息插入到数据库中。然后,您应该能够点击刷新按钮并让它显示所有玩家,但它只显示一个空行文本。发布到堆栈跟踪也没有错误,这使得发现问题变得更加困难。
数据库文件
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.TextView;
public class StatbookDB {
public static final String KEY_ROWID = "_id";
public static final String KEY_TEAMNAME = "team_name";
public static final String KEY_PLAYERS = "number_of_players";
public static final String KEY_PNUMBER = "players_number";
public static final String KEY_MASCOT = "team_mascot";
public static final String KEY_GENDER = "team_gender";
public static final String KEY_PNAME = "players_name";
public static final String KEY_2PTSMK= "field_goals_made";
public static final String KEY_2PTSMIS = "field_goals_missed";
public static final String KEY_2PTSTOTAL = "total_field_goals";
public static final String KEY_3PTSMK = "made_3pts";
public static final String KEY_3PTSMIS = "missed_3pts";
public static final String KEY_3PTSTOTAL = "total_3pts";
public static final String KEY_FTAT = "free_throws_attempted";
public static final String KEY_FTMK = "free_throws_missed";
public static final String KEY_FTTOTAL = "free_throws_total";
public static final String KEY_OREB = "offensive_rebounds";
public static final String KEY_DREB = "defensive_rebounds";
public static final String KEY_ASSISTS = "player_assits";
public static final String KEY_FOULS = "player_fouls";
public static final String KEY_TURNOVERS = "player_turnovers";
public static final String KEY_BLOCKS = "player_blocks";
public static final String KEY_STEALS = "player_steals";
public static final String KEY_INOUT = "in_or_out";
private String DATABASE_NTTABLE = "";
private static final String DATABASE_NAME = "BasketballStats";
private static final String DATABASE_TABLET = "teams";
private static final String DATABASE_TABLEH = "hometeam";
private static final String DATABASE_TABLEA = "awayteam";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLET + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_TEAMNAME + " TEXT NOT NULL, " +
KEY_MASCOT + " TEXT NOT NULL, " +
KEY_GENDER + " TEXT NOT NULL);"
);
/*db.execSQL("CREATE TABLE " + DATABASE_TABLEH + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_PNUMBER + " INTEGER NOT NULL, " +
KEY_PNAME + " TEXT NOT NULL, " +
KEY_2PTSMK + " INTEGER NOT NULL, " +
KEY_2PTSMIS + " INTEGER NOT NULL, " +
KEY_2PTSTOTAL + " INTEGER NOT NULL, " +
KEY_3PTSMK + " INTEGER NOT NULL, " +
KEY_3PTSMIS + " INTEGER NOT NULL, " +
KEY_3PTSTOTAL + " INTEGER NOT NULL, " +
KEY_FTMK + " INTEGER NOT NULL, " +
KEY_FTAT + " INTEGER NOT NULL, " +
KEY_FTTOTAL + " INTEGER NOT NULL, " +
KEY_OREB + " INTEGER NOT NULL, " +
KEY_DREB + " INTEGER NOT NULL, " +
KEY_ASSISTS + " INTEGER NOT NULL, " +
KEY_FOULS + " INTEGER NOT NULL, " +
KEY_TURNOVERS + " INTEGER NOT NULL, " +
KEY_BLOCKS + " INTEGER NOT NULL, " +
KEY_STEALS + " INTEGER NOT NULL, " +
KEY_INOUT + " TEXT NOT NULL);"
);*/
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLET);
onCreate(db);
}
}
public StatbookDB(Context c){
ourContext = c;
}
public StatbookDB open() {
ourHelper = new DbHelper(ourContext);
ourDatabase= ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long addTeam(String teamName, String teamMascot, String Gender) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_TEAMNAME, teamName);
cv.put(KEY_MASCOT, teamMascot);
cv.put(KEY_GENDER, Gender);
DATABASE_NTTABLE = teamName.replaceAll(" ", "_").toLowerCase(Locale.getDefault());
ourDatabase.execSQL("CREATE TABLE " + DATABASE_NTTABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_PNUMBER + " INTEGER NOT NULL, " +
KEY_PNAME + " TEXT NOT NULL, " +
KEY_2PTSMK + " INTEGER NOT NULL, " +
KEY_2PTSMIS + " INTEGER NOT NULL, " +
KEY_2PTSTOTAL + " INTEGER NOT NULL, " +
KEY_3PTSMK + " INTEGER NOT NULL, " +
KEY_3PTSMIS + " INTEGER NOT NULL, " +
KEY_3PTSTOTAL + " INTEGER NOT NULL, " +
KEY_FTMK + " INTEGER NOT NULL, " +
KEY_FTAT + " INTEGER NOT NULL, " +
KEY_FTTOTAL + " INTEGER NOT NULL, " +
KEY_OREB + " INTEGER NOT NULL, " +
KEY_DREB + " INTEGER NOT NULL, " +
KEY_ASSISTS + " INTEGER NOT NULL, " +
KEY_FOULS + " INTEGER NOT NULL, " +
KEY_TURNOVERS + " INTEGER NOT NULL, " +
KEY_BLOCKS + " INTEGER NOT NULL, " +
KEY_STEALS + " INTEGER NOT NULL, " +
KEY_INOUT + " TEXT NOT NULL);");
return ourDatabase.insert(DATABASE_TABLET, null, cv);
}
public String[] countTeams(String[] teams) {
String [] columns = {KEY_ROWID, KEY_TEAMNAME};
Cursor c = ourDatabase.query(DATABASE_TABLET, columns, null, null, null, null, null);
int iRow = c.getColumnIndex(KEY_ROWID);
int iTeam = c.getColumnIndex(KEY_TEAMNAME);
List<String> teams3 = new ArrayList<String>();
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
String s = c.getString(iTeam);
int i = c.getInt(iRow);
teams3.add(s);
}
String[] teams2 = new String[teams3.size()];
teams3.toArray(teams2);
// TODO Auto-generated method stub
return teams2;
}
public String getPlayers(String teamName) {
// TODO Auto-generated method stub
String TABLE = teamName.toLowerCase(Locale.getDefault()).replaceAll(" ", "_");
String [] columns = {KEY_PNUMBER, KEY_PNAME};
Cursor c = ourDatabase.query(TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iNum = c.getColumnIndex(KEY_PNUMBER);
int iName = c.getColumnIndex(KEY_PNAME);
if (c.getCount()!= 0){
try {
for (c.moveToFirst(); c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iRow) + " " + c.getString(iNum) + " " + c.getString(iName) + "\n";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
result = "No players added to this team yet";
}
return result;
}
public long addPlayer(String playerName, int playerNumber, String teamName) {
// TODO Auto-generated method stub
int dValue = 0;
String dbTable = teamName.toLowerCase(Locale.getDefault()).replaceAll(" ", "_");
ContentValues cv = new ContentValues();
cv.put(KEY_PNUMBER, playerNumber);
cv.put(KEY_PNAME, playerName);
cv.put(KEY_2PTSMK, dValue);
cv.put(KEY_2PTSMIS, dValue);
cv.put(KEY_2PTSTOTAL, dValue);
cv.put(KEY_3PTSMK, dValue);
cv.put(KEY_3PTSMIS, dValue);
cv.put(KEY_3PTSTOTAL, dValue);
cv.put(KEY_FTMK, dValue);
cv.put(KEY_FTAT, dValue);
cv.put(KEY_FTTOTAL, dValue);
cv.put(KEY_OREB, dValue);
cv.put(KEY_DREB, dValue);
cv.put(KEY_ASSISTS, dValue);
cv.put(KEY_FOULS, dValue);
cv.put(KEY_TURNOVERS, dValue);
cv.put(KEY_BLOCKS, dValue);
cv.put(KEY_STEALS, dValue);
cv.put(KEY_INOUT, "out");
return ourDatabase.insert(dbTable, null, cv);
}
}
添加和显示播放器的文件
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
public class TeamView extends Activity implements OnClickListener {
TabHost th;
TabSpec ts;
Button addPlayer, refresh;
EditText Pname, Pnum;
TextView showPlayers, tName;
String teamName, players, teamNameUN;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.teamview);
Initialize();
Bundle getTeam = getIntent().getExtras();
teamName = getTeam.getString("team");
teamNameUN = getTeam.getString("teamUN");
th.setup();
ts = th.newTabSpec("tag1");
ts.setContent(R.id.tab1);
ts.setIndicator("View Team");
th.addTab(ts);
ts = th.newTabSpec("tag2");
ts.setContent(R.id.tab2);
ts.setIndicator("Add Player");
th.addTab(ts);
tName.setText(teamName);
StatbookDB count = new StatbookDB(this);
count.open();
players = count.getPlayers(teamNameUN);
count.close();
showPlayers.setText(players);
}
private void Initialize() {
// TODO Auto-generated method stub
th = (TabHost)findViewById(R.id.tabhost);
addPlayer = (Button)findViewById(R.id.bAddPlayer);
refresh = (Button)findViewById(R.id.bRefresh);
Pname = (EditText)findViewById(R.id.etPName);
Pnum = (EditText)findViewById(R.id.etPNum);
showPlayers = (TextView)findViewById(R.id.tvShowPlayers);
tName = (TextView)findViewById(R.id.tvTname);
addPlayer.setOnClickListener(this);
refresh.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.bAddPlayer:
String playerName = Pname.getText().toString();
String plNum = Pnum.getText().toString();
int playerNumber;
playerNumber = Integer.parseInt(plNum);
StatbookDB entry = new StatbookDB(this);
entry.open();
entry.addPlayer(playerName, playerNumber, teamNameUN);
entry.close();
break;
case R.id.bRefresh:
StatbookDB count = new StatbookDB(this);
count.open();
players = count.getPlayers(teamNameUN);
count.close();
showPlayers.setText(players);
break;
}
}
}
答案 0 :(得分:0)
我认为问题出在getPlayers
类的StatbookDB
函数中:
for (c.moveToFirst(); c.isAfterLast(); c.moveToNext()){
虽然它应该是for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
循环表达式是反向的
除此之外,您应该在KEY_ROWID
中添加columns
,因为它会String [] columns = {KEY_ROWID, KEY_PNUMBER, KEY_PNAME};
,因为您稍后会收到