代码之家  ›  专栏  ›  技术社区  ›  Tom

在android中动态显示更改列表项的最佳方法

  •  1
  • Tom  · 技术社区  · 6 年前

    我对编码和android是新手,这是我的第一篇帖子。。。

    我正在制作一个android应用程序,试图显示一个值列表,当用户从菜单中选择时,这些值会动态变化。起初,我只是显示文本视图,但需要一种更简洁的方法。我尝试过使用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)
        );
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Rahul Yadav    6 年前

    只需为listview创建一个自定义适配器。你可以按照这个 link .

    之后,只需在适配器上调用notifyDataSetChanged()即可更改数据。