android - 在android中显示动态变化的列表项的最佳方式

标签 android listview textview

我是编码和 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 制作自定义适配器即可。您可以按照此 link .

之后,您可以通过在适配器上调用 notifyDataSetChanged() 来简单地更改数据。

关于android - 在android中显示动态变化的列表项的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49546958/

相关文章:

java - 如何在 Android Studio 中更新 Cordova 版本

android - 如何在 TextView 中启用 'fling scrolling'

java - 如何使用另一个按钮创建一个按钮

android - 如何在 Kotlin 中使用 intent.setComponent

android - 在 Android 中使用 YouTube Data API v3 可恢复 YouTube 上传

android - 如何在android中的数组适配器中设置文本颜色

java - Android - 使用 Volley 显示自定义 ListView (使用 http 请求发送 header )

android - 如何在android中创建自定义 ListView

android - 如何在Android的textview上删除顶部和底部空间

android - setText 使应用程序意外停止