我是编码和android的新手,这是我的第一篇文章...
我正在创建一个Android应用程序,并且我正在尝试显示当用户从菜单中选择时动态更改的值列表。最初我只是显示TextViews但想要更清洁的方法。我已经尝试过使用ListView但不知道如何更改列表项'文本动态。
以下是目前的做法:
public class WeaponTypeListener implements AdapterView.OnItemSelectedListener {
private TextView currentWeaponDPS;
private TextView currentWeaponDamage;
private TextView currentWeaponFireRate;
private TextView currentWeaponMagazineSize;
private TextView currentWeaponReloadTime;
private TextView currentWeaponRarity;
private TextView currentWeaponBulletType;
public WeaponTypeListener(TextView currentWeaponDPS,
TextView currentWeaponDamage,
TextView currentWeaponFireRate,
TextView currentWeaponMagazineSize,
TextView currentWeaponReloadTime,
TextView currentWeaponRarity,
TextView currentWeaponBulletType
){
this.currentWeaponDPS = currentWeaponDPS;
this.currentWeaponDamage = currentWeaponDamage;
this.currentWeaponFireRate = currentWeaponFireRate;
this.currentWeaponMagazineSize = currentWeaponMagazineSize;
this.currentWeaponReloadTime = currentWeaponReloadTime;
this.currentWeaponRarity = currentWeaponRarity;
this.currentWeaponBulletType = currentWeaponBulletType;
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String selection = Weapons.ALL_WEAPON_TYPES.get(i);
Weapon stats = Weapons.getWeaponStats(selection);
this.setWeaponStats(stats);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
private void setWeaponStats (Weapon w) {
this.currentWeaponDPS.setText(Double.toString(w.getDps()));
this.currentWeaponDamage.setText(Integer.toString(w.getDamage()));
this.currentWeaponFireRate.setText(Double.toString(w.getFireRate()));
this.currentWeaponMagazineSize.setText(Integer.toString(w.getMagazineSize()));
this.currentWeaponReloadTime.setText(Double.toString(w.getReloadTime()));
this.currentWeaponRarity.setText(w.getRarity());
this.currentWeaponBulletType.setText(w.getBulletType());
}
}
还有:
public class WeaponComparisonActivity extends AppCompatActivity {
@BindView(R.id.weaponTypeA) Spinner weaponTypeA;
@BindView(R.id.weaponTypeB) Spinner weaponTypeB;
@BindView(R.id.weaponSelectionA) TextView weaponSelectionA;
@BindView(R.id.currentWeaponDPS) TextView currentWeaponDPS;
@BindView(R.id.currentWeaponDamage) TextView currentWeaponDamage;
@BindView(R.id.currentWeaponFireRate) TextView currentWeaponFireRate;
@BindView(R.id.currentWeaponMagazineSize) TextView currentWeaponMagazineSize;
@BindView(R.id.currentWeaponReloadTime) TextView currentWeaponReloadTime;
@BindView(R.id.currentWeaponRarity) TextView currentWeaponRarity;
@BindView(R.id.currentWeaponBulletType) TextView currentWeaponBulletType;
@BindView(R.id.potentialWeaponDPS) TextView potentialWeaponDPS;
@BindView(R.id.potentialWeaponDamage) TextView potentialWeaponDamage;
@BindView(R.id.potentialWeaponFireRate) TextView potentialWeaponFireRate;
@BindView(R.id.potentialWeaponMagazineSize) TextView potentialWeaponMagazineSize;
@BindView(R.id.potentialWeaponReloadTime) TextView potentialWeaponReloadTime;
@BindView(R.id.potentialWeaponRarity) TextView potentialWeaponRarity;
@BindView(R.id.potentialWeaponBulletType) TextView potentialWeaponBulletType;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weapon_comparison_activity);
ButterKnife.bind(this);
ArrayAdapter<String> weaponTypeAdapter = new ArrayAdapter<String>(WeaponComparisonActivity.this, android.R.layout.simple_spinner_dropdown_item, Weapons.ALL_WEAPON_TYPES);
weaponTypeA.setAdapter(weaponTypeAdapter);
weaponTypeB.setAdapter(weaponTypeAdapter);
weaponTypeA.setOnItemSelectedListener(new WeaponTypeListener(
currentWeaponDPS,
currentWeaponDamage,
currentWeaponFireRate,
currentWeaponMagazineSize,
currentWeaponReloadTime,
currentWeaponRarity,
currentWeaponBulletType)
);
weaponTypeB.setOnItemSelectedListener(new WeaponTypeListener(
potentialWeaponDPS,
potentialWeaponDamage,
potentialWeaponFireRate,
potentialWeaponMagazineSize,
potentialWeaponReloadTime,
potentialWeaponRarity,
potentialWeaponBulletType)
);
}