简体   繁体   English

Android:ListView的问题(使用意图从另一个活动中将值更新/传递给它)

[英]Android: Problems With ListView (Updating/Passing Values Into It From Another Activity Using Intent)

I have an activity and upon a certain condition, it will pass an integer value to the activity that contains the ListView. 我有一个活动,在一定条件下,它将把一个整数值传递给包含ListView的活动。 However, I am running into several problems: 但是,我遇到了几个问题:

  1. There is an initial value of 0 and when the activity is performed and the condition is met another 0 gets passed in, instead of the actual value. 初始值为0,并且在执行活动并满足条件时,将传入另一个0,而不是实际值。

  2. The ListView is reset every time it is run from the main activity. 每次从主活动运行ListView时,都会重置它。 I would like it to keep the results from previous runs (until a user hits a reset button on the ListView activity itself). 我希望它保留以前运行的结果(直到用户单击ListView活动本身上的Reset按钮)。

**EDIT 3. I have noticed behavior that I did not notice before and that is that every time the ListView activity is opened from the main activity, another 0 is added to the ListView. **编辑3.我注意到以前没有注意到的行为,那就是每次从主活动打开ListView活动时,都会向ListView添加另一个0。

Here is the relevant code - 这是相关的代码-

Activity Passing Value to Activity Containing ListView: 活动传递值到包含ListView的活动:

if(condition) {
    textview.setText("String1");
    Intent intent = new Intent(this, ListViewActivity.class);
    intent.putExtra("numCounter", numCounter);
}

Activity with ListView: ListView活动:

ListView numListView;
ArrayAdapter<Integer> listAdapter; 

static ArrayList<Integer> numArrayList = new ArrayList<Integer>();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_name);

    Intent intent = getIntent();
    int intentNum = intent.getIntExtra("numCounter", 0);
    addNumArrayList(intentNum);

    numListView = (ListView) findViewById(R.id.numberListView);

    listAdapter = new ArrayAdapter<Integer>(this, R.layout.listview_row, numArrayList);

    numListView.setAdapter(listAdapter); 
}

// Keep ArrayList to size of 10, while keeping it sorted 
// (displaying only the 10 lowest)

public static void addNumArrayList(int val) {
    numArrayList.add(val);
    Collections.sort(numArrayList);
    if(numArrayList.size() > 10) {
        for(int i = 10; i < numArrayList.size(); i++) {
            numArrayList.remove(i);
        }
    }
}

I basically would like it so that only the other activity or activities can alter it, except when the reset button is hit on the ListView activity itself (simply opening the activity from the main activity should do nothing). 我基本上希望这样做,以便只有其他一个或多个活动才能更改它,除非在ListView活动本身上按了reset按钮时(从主活动中打开活动应该什么也不做)。

How do I fix it? 我如何解决它?

Add below condition for addnumArrayList 为addnumArrayList添加以下条件

Bundle mBundle = intent.getExtras();
if(mBundle != null && mBundle.containsKey(¨numCounter¨)){
    int intentNum = intent.getIntExtra("numCounter", 0);
    addNumArrayList(intentNum);
}

You are getting the default value of 0 even if the key is not present. 即使键不存在,您也会获得默认值0。 So do check if the key is present 因此,请检查密钥是否存在

As far as the listView getting reset, you should store values in a file rather than in a static variable, the values will be reset if the process is killed. 至于listView重置,您应该将值存储在文件中,而不是存储在静态变量中,如果进程被杀死,则将重置这些值。

Edit: Try this 编辑:试试这个

   int intentNum = intent.getIntExtra("numCounter", -1); // -1 or any number that you do not pass
   if(intentNum != -1)
    addNumArrayList(intentNum);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM