【问题标题】:How can i update an item in a string array and update the ListView accordingly?如何更新字符串数组中的项目并相应地更新 ListView?
【发布时间】:2015-09-25 05:25:12
【问题描述】:

我有一个代码,我最初使用自定义适配器使用字符串数组填充列表视图。我想相应地更新字符串数组中的字段,从而更新列表视图,有没有办法编辑字符串数组?问题是我的字符串数组不仅包含一个字符串,它还包含一个 int 字段,我将 acc 更新给用户。

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:如果他们无法提供帮助,我不明白为什么人们会投反对票。这里并不是每个人都是丹尼斯·里奇。

【问题讨论】:

  • 投反对票可能是因为,您没有提到您遇到的错误,而且 logcat 也丢失了。
  • @Prera​​kSola 也许是因为我没有收到任何错误?我特别要求一种更新列表视图的方法,我没有提到我有任何错误。如果您给出答案并且答案似乎很容易,那么投票似乎是可以接受的。如果人们投反对票并且问题仍未得到解答,那就太愚蠢了。如果人们在这里得不到帮助,我认为堆栈社区本身就有问题。
  • 好吧,让你知道,这里的工作方式有点不同。在这里,我们帮助那些无法解决问题的人。这既不是编码服务,也不是搜索/研究引擎。你需要自己做这些事情。

标签: android arrays listview android-listview


【解决方案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”是该值在列表中的位置。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      • 2013-11-10
      • 2023-04-11
      相关资源
      最近更新 更多