【发布时间】:2018-03-29 02:12:42
【问题描述】:
我是编码和 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)
);
}
【问题讨论】:
-
先明确listView的概念。您不需要在适配器中膨胀多个 textView。你已经清除了listView的概念。有一种方法可以通知列表视图中的更改“notifyDatasetChanged”将是您需要调用的方法。