如何更新字符串数组中的项目并相应地更新ListView?

时间:2015-07-07 15:46:18

标签: android arrays listview android-listview

我有一个代码,我最初使用自定义适配器使用字符串数组填充listview。我想相应地更新字符串数组中的字段,因此更新listview,有没有办法编辑字符串数组?问题是我的字符串数组不只是一个字符串,它包含一个int字段,我根据用户更新。

import android.app.ActionBar;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.NumberPicker;
import android.widget.Toast;
import android.widget.Toolbar;


public class CashRegisterActivity extends Activity {

    NumberPicker numberPicker;
    EditText magnitude;
    ListView cashRegisterListview;
    int a0=0,a1=0,a2=0,a3=0,a4=0,a5=0,a6=0,a7=0,a8=0,a9=0;
    String[] list= {"1000 x " + a0, "500 x " + a1, "100 x " + a2, "50 x " + a3, "20 x " + a4,
            "10 x " + a5, "5 x " + a6, "2 x " + a7, "1 x " + a8, "0.5 x " + a9};
    CustomCashlistAdapter customCashlistAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cash_register);
        Toolbar tb = (Toolbar)findViewById(R.id.tool_bar);
        setActionBar(tb);
        cashRegisterListview = (ListView)findViewById(R.id.cashRegisterListview);
        magnitude=(EditText)findViewById(R.id.magnitude);
        numberPicker = (NumberPicker)findViewById(R.id.numberPicker);
        numberPicker.setDisplayedValues(getResources().getStringArray(R.array.inr_picker_display));
        numberPicker.setEnabled(true);
        numberPicker.setMaxValue(getResources().getStringArray(R.array.inr_picker_display).length - 1);
        numberPicker.setMinValue(0);
        customCashlistAdapter = new CustomCashlistAdapter(this,list);
        cashRegisterListview.setAdapter(customCashlistAdapter);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("Cash Register");
        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();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        else if (id == android.R.id.home){
            finish();
        }

        return super.onOptionsItemSelected(item);
    }


    //Add Selection
    public void addSelection(View view){
        double currentSelection = 0;
        int currentMagnitude = Integer.parseInt(magnitude.getText().toString());
        switch (numberPicker.getValue()){
            case 0:
                currentSelection=1000;a0=currentMagnitude;Toast.makeText(this,""+a0,Toast.LENGTH_SHORT).show();
                break;
            case 1:
                currentSelection=500;a1=currentMagnitude;
                break;
            case 2:
                currentSelection=100;a2=currentMagnitude;
                break;
            case 3:
                currentSelection=50;a3=currentMagnitude;
                break;
            case 4:
                currentSelection=20;a4=currentMagnitude;
                break;
            case 5:
                currentSelection=10;a5=currentMagnitude;
                break;
            case 6:
                currentSelection=5;a6=currentMagnitude;
                break;
            case 7:
                currentSelection=2;a7=currentMagnitude;
                break;
            case 8:
                currentSelection=1;a8=currentMagnitude;
                break;
            case 9:
                currentSelection=0.5;a9=currentMagnitude;
                break;
        }
        customCashlistAdapter.notifyDataSetChanged();
    }
}
PS:我不知道为什么人们如果无能为力就会投票。并不是每个人都是Dennis Ritchie。

2 个答案:

答案 0 :(得分:1)

你可以借助第二个清单来做到这一点:

String[] helperlist = {"1000 x ", "500 x ", "100 x ", "50 x ", "20 x ", "10 x ", "5 x ", "2 x ", "1 x ", "0.5 x "};
list[x] = helperlist[x] + ax;

其中" ax"是你改变的价值和" x"是您列表中该值的位置。

答案 1 :(得分:0)

考虑使用更复杂的集合类,如ArrayList,您可以通过单个调用添加,删除,修剪和许多其他内容。

相关问题