我正在尝试在我的一个活动中添加一个微调器,我可以从一组字段中动态地向微调器添加项目,这样当我点击该微调器中的一个项目时,它会重新填充这些字段。将字段中的项添加到侦听器工作正常,但是当我尝试单击微调器以重新填充字段时。它仍然是一项正在进行中的工作,因此存在过多的代码。
这是正在运行的类的代码
package com.SpurFlys.pocketscout;
import java.util.*;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class TeamSelection extends Activity implements OnItemSelectedListener{
private Spinner spinner;
private static final List<String> paths = new ArrayList<String>();
private String tournamentName;
Tournament tournament = new Tournament();
EditText mEdit1;
EditText mEdit2;
EditText mEdit3;
EditText mEdit4;
EditText mEdit5;
EditText mEdit6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_team_selection);
Intent intent = getIntent();
tournamentName = intent.getExtras().getString("tournamentName");
tournament.name = tournamentName;
spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String>adapter = new ArrayAdapter<String>(TeamSelection.this, android.R.layout.simple_spinner_item,paths);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.team_selection, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
String teamNumber = tournament.teamIndex.get(position);
mEdit1.setText(teamNumber);
String[] temp = tournament.teams.get(teamNumber);
String auton = temp[0];
mEdit2.setText(auton);
String chassis = temp[1];
mEdit3.setText(chassis);
String arm = temp[2];
mEdit4.setText(arm);
String intake = temp[3];
mEdit5.setText(intake);
String other = temp[4];
mEdit6.setText(other);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
public void addTeam(View view){
mEdit1 = (EditText)findViewById(R.id.editText1);
mEdit2 = (EditText)findViewById(R.id.editText2);
mEdit3 = (EditText)findViewById(R.id.editText3);
mEdit4 = (EditText)findViewById(R.id.editText4);
mEdit5 = (EditText)findViewById(R.id.editText5);
mEdit6 = (EditText)findViewById(R.id.editText6);
String teamNumber = mEdit1.getText().toString();
String auton = mEdit2.getText().toString();
String chassis = mEdit3.getText().toString();
String arm = mEdit4.getText().toString();
String intake = mEdit5.getText().toString();
String other = mEdit6.getText().toString();
if(teamNumber.trim().equals("")){
Toast.makeText(this, "Blank is not a valid team number", Toast.LENGTH_LONG).show();
}
else{
paths.add(teamNumber);
tournament.createTeam(teamNumber, auton, chassis, arm, intake, other, paths.indexOf(teamNumber));
mEdit1.setText("");
mEdit2.setText("");
mEdit3.setText("");
mEdit4.setText("");
mEdit5.setText("");
mEdit6.setText("");
}
}
}
这是活动的代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/teams"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.SpurFlys.pocketscout.TeamSelection" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="11dp"
android:onClick="addTeam"
android:text="@string/add_team" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:ems="10"
android:hint="@string/team_number" />
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:contentDescription="@string/teams" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:ems="10"
android:hint="@string/auton"
android:inputType="textMultiLine" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:ems="10"
android:hint="@string/chassis"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText3"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:ems="10"
android:hint="@string/arm"
android:inputType="textMultiLine" />
<EditText
android:id="@+id/editText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText4"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:ems="10"
android:hint="@string/intake"
android:inputType="textMultiLine" />
<EditText
android:id="@+id/editText6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText5"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:ems="10"
android:hint="@string/other"
android:inputType="textMultiLine" />
</RelativeLayout>
这是名为
的锦标赛对象的代码package com.SpurFlys.pocketscout;
import java.util.*;
public class Tournament {
public String name;
public String date;
public String address;
public String city;
public String state;
public String country;
public String postalCode;
public Map<String, String[]> teams;
public Map<String, String> teamIndex = new HashMap<String, String>();
String[] tempTeam;
public Tournament() {
teams = new HashMap<String, String[]>();
tempTeam = new String[5];
}
public void createTeam(String number, String auton, String chassis, String arm, String intake, String other, int index){
teamIndex.put(Integer.valueOf(index).toString(), number);
tempTeam[0] = auton;
tempTeam[1] = chassis;
tempTeam[2] = arm;
tempTeam[3] = intake;
tempTeam[4] = other;
teams.put(number, tempTeam);
}
}
答案 0 :(得分:0)
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
//you should be switching on the view, since the class is implementing onItemSelectedListenter
switch(v.getId()){
case (R.id.spinner):
//logic for setting mEdit1.setText(teamNumber), etc..
break;
}
}
由于您为活动实现了onItemSelectedListener,因此必须设置表单对象在选中时应执行的操作的情况。说你有另一个旋转器。该微调器也将this
作为设置onItemSelectedListener
的arg。应用程序无法知道每个实例应该做什么,除非它知道选择了哪个view
。