【问题标题】:update a variable in another activity更新另一个活动中的变量
【发布时间】:2014-04-22 12:27:15
【问题描述】:

大家好,感谢您的关注,希望您能找到答案……我正在尝试更新高分屏幕。 这是我的适配器

SimpleAdapter adapter1 = new SimpleAdapter(
                this,
                listGeneral,
                R.layout.list_highscore_row,
                new String[] {"catGeneral","score1"},
                new int[] {R.id.text1,R.id.text2}
        );

这填满了我的清单

HashMap<String,String> temp = new HashMap<String,String>();
        temp.put("catGeneral","Level 1");
        temp.put("score1", String.valueOf(hScoreGen1) + "/10");
        listGeneral.add(temp);

我想更改hScoreGen1 的值,如果它低于新分数... 到目前为止,我在另一个应该改变 hScoreGen1 值的活动中有这个

if (totalCorrect > ScoreScreen.currentScoreCatValue){  //currentScoreCatValue is the orginal value of hScoreGen1
                            if (currentScoreCat == 1){   //currentScoreCat is set to 1 from another activity
                                HighScores.hScoreGen1 = totalCorrect;

我的问题似乎是列表视图没有显示更改的值,而是显示 onCreate 值...希望您能提供帮助 如果需要,我可以添加更多代码

【问题讨论】:

  • 您必须刷新列表视图?通知数据集更改()。更多信息Here 还确保您从主 UI 线程调用它。
  • 致电notifyDataSetChanged()
  • 使用 notifydatachanged() 更新您的 HashMap 和 SimpleAdapter
  • 不要让你的适配器静态,可能会发生泄漏。而是实现广播和接收器来实现这个,或者监听器。
  • @jitain 你能给我代码示例吗,我不知道如何从其他活动中调用适配器,而不是通过静态

标签: android listview android-activity


【解决方案1】:

更新你的适配器

    if (totalCorrect > ScoreScreen.currentScoreCatValue){  //currentScoreCatValue is the         orginal value of hScoreGen1
                        if (currentScoreCat == 1){   //currentScoreCat is set to 1 from another activity
                            HighScores.hScoreGen1 = totalCorrect;
                            HighScores.adapter1.notifydatachanged();

【讨论】:

  • 抱歉 karthick notifydatachanged();导致方法 notifydatachanged() 未定义 SimpleAdapter 类型错误并要求更改为 notifydatasetchanged() 也不起作用
  • 我可以轻松更换简单的适配器
  • public void setNewHighScore(){ if (totalCorrect > ScoreScreen.currentScoreCatValue){ 导致 nullpointerexemption HighScores.hScoreGen1 = totalCorrect; HighScores.adapter1.notifyDataSetChanged(); } }
猜你喜欢
  • 1970-01-01
  • 2015-02-04
  • 2015-02-16
  • 1970-01-01
  • 1970-01-01
  • 2019-05-02
  • 1970-01-01
  • 2012-04-15
  • 1970-01-01
相关资源
最近更新 更多