根据SQLite表

时间:2016-04-16 07:48:31

标签: android android-studio android-spinner

我是Android编程的新手,所以对任何滥用技术术语表示歉意。我有一个微调器,其文本值为league_name,id id来自leagues SQLite表。

联赛表设计示例

id  |  league_name
-----------------
1   |  Northern Premier Division
2   |  Southern League 1
3   |  Northern Division 2

我还有一个teams表,用于存储团队信息,以及一个查询表,用于连接名为teams的{​​{1}}和leagues表的ID。因此,此微调器的选定值将插入teams_vs_leagues表中的league_id列。我有一个编辑页面来编辑单个团队记录,其中包括从微调器中选择一个联盟。但是,在这种情况下teams_vs_leagues始终默认为第一个可用id。虽然,我希望微调器的初始值是1中与您当前正在查看的league_id对应的选定teams_vs_leagues。因此,如果所选team_id的{​​{1}} team_idleague_id,我希望微调器的初始值为3

UpdateTeam类

Northern Division 2

联赛班级

public class UpdateTeam extends AppCompatActivity {

 ArrayList < Team > imageArry = new ArrayList < Team > ();
 TeamUpdateAdapter adapter;
 Button updateButton;
 DatabaseHelper myDb;
 EditText teamNameEdit;
 ImageView teamImage;
 Spinner league; //Create Spinner Variables to cast in OnCreate method

 ArrayList < String > leagues = new ArrayList < String > (); //Create Array List to bind to Spinner
 ArrayAdapter < String > arrayAdapter; //Declare Array Adapter

 long leagueId; //Declare global long for league_id
 long id; //Declare global long for team_id

 String picturePath = ""; //Create String variable for image path to be stored in db table

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.edit_team);

  myDb = new DatabaseHelper(this);

  updateButton = (Button) findViewById(R.id.btnUpdate);

  //Call Spinner Variable and cast spinner id reference
  league = (Spinner) findViewById(R.id.edit_league_id);

  //Call Array Adapter to Bind Array list data to Spinner
  arrayAdapter = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, leagues);

  //get selected teams values from intent
  id = getIntent().getLongExtra("TEAM", 0 l);
  final String editTeamName = getIntent().getStringExtra("TEAM_NAME");
  picturePath = getIntent().getStringExtra("IMAGE");
  leagueId = getIntent().getLongExtra("LEAGUE_ID", 01);
  //Toast.makeText(getBaseContext(), "Leagueid = " + leagueId, Toast.LENGTH_LONG).show();
  teamNameEdit = (EditText) findViewById(R.id.eTeamName);
  teamNameEdit.setText(editTeamName, TextView.BufferType.EDITABLE);

  // Reading all teams from database
  final List < Team > team = myDb.getTeam((int) id);
  for (Team tm: team) {
   String log = "ID:" + tm.getId() + " Team Name: " + tm.getTeamName() + " ,Image: " + tm.getPath() + " ,Points: " + tm.getPoints() + " ,League Name: " + tm.getLeagueName() + " ,League ID: " + tm.getLeagueId();

   // Writing teams to log
   Log.d("Result: ", log);
   //add teams data in arrayList
   imageArry.add(tm);
  }
  adapter = new TeamUpdateAdapter(this, R.layout.update_team,
   imageArry);
  ListView dataList = (ListView) findViewById(R.id.main_list_view);
  dataList.setAdapter(adapter);

  //call loadSpinnerData method
  loadSpinnerData();

  //call updateTeam
  updateTeam();

  //call selectLeague
  selectLeague();


 }

 private void loadSpinnerData() {
  // Spinner Drop down elements
  List < Leagues > leagueList = myDb.getAllLeagues();
  // Creating adapter for spinner
  ArrayAdapter < Leagues > adapter = new ArrayAdapter < Leagues > (this,
   android.R.layout.simple_spinner_item, leagueList);
  // Drop down layout style - list view with radio button
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  // attaching data adapter to spinner
  league.setAdapter(adapter);
 }

 public void selectLeague() {
  //set onItemSelected Listener for spinner
  league.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
   @Override
   public void onItemSelected(AdapterView < ? > parent, View view, int position, long id) {

    //get selected league id
    leagueId = Integer.parseInt(String.valueOf(((Leagues) league.getSelectedItem()).getId()));
   }

   @Override
   public void onNothingSelected(AdapterView < ? > parent) {

   }
  });
 }

 //Create addTeam method to add new team to db
 public void updateTeam() {
  updateButton.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    if (myDb.updateTeam(
      id,
      teamNameEdit.getText().toString(),
      picturePath,
      leagueId)) {

     Toast.makeText(getBaseContext(), "Update successful.", Toast.LENGTH_LONG).show();
     Intent intent =
      new Intent(UpdateTeam.this, DisplayTeam.class);
     startActivity(intent);
    } else
     Toast.makeText(getBaseContext(), "Update failed.", Toast.LENGTH_LONG).show();

   }
  });
 }
}

getAllLeagues方法

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;
 }
}

如果您需要任何进一步的信息,请不要犹豫,发表评论。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

将选定的联盟对象提供给数组适配器的getPosition()方法

int index = arrayAdapter.getPosition(SELECTED_LEAGUE_ID);
league.setSelection(index);