Android 根据SQLite表中选定的行和列值设置微调器的初始值
我是android编程新手,因此对任何误用技术术语表示歉意。我有一个微调器,它的文本值为Android 根据SQLite表中选定的行和列值设置微调器的初始值,android,android-studio,android-spinner,Android,Android Studio,Android Spinner,我是android编程新手,因此对任何误用技术术语表示歉意。我有一个微调器,它的文本值为leagues\u name,id为id,来自leaguesSQLite表 leagues表设计示例 我还有一个teams表,用于存储团队信息,还有一个查找表,用于连接teams和leagues的id,该表名为teams\u vs\u leagues。因此,此微调器的选定值将插入到球队vs联赛表中的联赛id列中。我有一个编辑页面来编辑单个球队的记录,其中包括从微调器中选择联赛。但是,在本例中,它总是默认为第一
leagues\u name
,id为id
,来自leagues
SQLite表
leagues表设计示例
我还有一个teams
表,用于存储团队信息,还有一个查找表,用于连接teams
和leagues
的id,该表名为teams\u vs\u leagues
。因此,此微调器的选定值将插入到球队vs联赛
表中的联赛id
列中。我有一个编辑页面来编辑单个球队的记录,其中包括从微调器中选择联赛。但是,在本例中,它总是默认为第一个可用的id
。尽管如此,我希望微调器的初始值是在与当前查看的团队id
相对应的团队id
中选择的联盟id
。因此,如果选定的球队id
的联盟id
为3
,我希望微调器的初始值为Northern Division 2
UpdateTeam类
getAllLeagues方法
public ListgetAllLeagues(){
列表标签=新阵列列表();
//选择所有查询
String selectQuery=“SELECT*FROM”+联赛表;
SQLiteDatabase db=this.getReadableDatabase();
Cursor Cursor=db.rawQuery(selectQuery,null);
//循环遍历所有行并添加到列表
if(cursor.moveToFirst()){
做{
labels.add(新的联盟(cursor.getInt(0)、cursor.getString(1));
}while(cursor.moveToNext());
}
//闭合连接
cursor.close();
db.close();
//退回标签
退货标签;
}
如果您需要任何进一步的信息,请随时发表评论。任何帮助都将不胜感激。将所选的league对象提供给数组适配器的getPosition()方法
int index = arrayAdapter.getPosition(SELECTED_LEAGUE_ID);
league.setSelection(index);
向数组适配器的getPosition()方法提供所选的league对象
int index = arrayAdapter.getPosition(SELECTED_LEAGUE_ID);
league.setSelection(index);
当我设置
int index=arrayAdapter.getPosition(String.valueOf(leagueId));联盟选举(指数)
我总是得到1
的值,尽管如果我选择了一个值应该是2
或3
的团队,我很抱歉,当我设置int index=arrayAdapter.getPosition(String.valueOf(leagueId)),我总是得到-1
的值;联盟选举(指数)代码>我总是得到一个值1
,尽管我选择了一个值应为2
或3的团队,但我的道歉是,我总是得到一个值-1
public class Leagues {
//Declare Global String & int
private int id;
private String leagueName;
/*********** Set Methods ******************/
public Leagues(int id, String leagueName) {
this.id = id;
this.leagueName = leagueName;
}
public void setId(int id) {
this.id = id;
}
public void setLeagueName(String leagueName) {
this.leagueName = leagueName;
}
/*********** Get Methods ****************/
public int getId() {
return this.id;
}
public String getLeagueName() {
return this.leagueName;
}
//Provides string value to display in Spinner
@Override
public String toString() {
return leagueName;
}
}
public List < Leagues > getAllLeagues() {
List < Leagues > labels = new ArrayList < Leagues > ();
// Select All Query
String selectQuery = "SELECT * FROM " + LEAGUES_TABLE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(new Leagues(cursor.getInt(0), cursor.getString(1)));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning labels
return labels;
}
int index = arrayAdapter.getPosition(SELECTED_LEAGUE_ID);
league.setSelection(index);