【问题标题】:Android Listview not refreshingAndroid Listview 不刷新
【发布时间】:2013-08-28 13:52:23
【问题描述】:

您好,我的程序应该根据选择的单选按钮更新列表视图,并显示两个表之一的所有记录。

据我所知,记录实际上已填充,似乎并没有刷新列表视图本身。请帮我解决这个问题。提前致谢

这是我的代码:

在 onCreate 中:

RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                RadioButton rb1 = (RadioButton) findViewById(R.id.radio0);
                RadioButton rb2 = (RadioButton) findViewById(R.id.radio1);

                switch(checkedId)
                {
                case R.id.radio0:
                    {
                        rb2.setChecked(false);          
//                      populateList();
//                      ListView lview = (ListView) findViewById(R.id.listView1);
//                      lview.invalidateViews();
                    }
                    break;
                case R.id.radio1:
                {
                        rb1.setChecked(false);
//                      populateList();
//                      ListView lview = (ListView) findViewById(R.id.listView1);
//                      lview.invalidateViews();
                }
                    break;
                }
                populateList();  
//              ListView lview = (ListView) findViewById(R.id.listView1);
//              lview.updateViewLayout();
            }


        });   

使用的方法:

 private void populateList() 
    {
        RadioButton rb1 = (RadioButton) findViewById(R.id.radio0);
        RadioButton rb2 = (RadioButton) findViewById(R.id.radio1);
        EditText mEdit1 = (EditText)findViewById(R.id.editText1);

        list = new ArrayList<HashMap>();
        insertConstant();

        if(rb1.isChecked() == true)
        {
             auditds = new ProjectAuditingDataSource(this);
                auditds.open();

                List<DataCapture> auditValues = auditds.getAllAudits();
                ArrayAdapter<DataCapture> auditadapter = new ArrayAdapter<DataCapture>(this,
                        android.R.layout.simple_list_item_1, auditValues);

                int countaudits = 0; 
                int auditsize = auditValues.size();

                while (countaudits < auditsize)
                {
                    HashMap temp2 = new HashMap();          

                    temp2.put(FIRST_COLUMN,auditValues.get(countaudits).getId());
                    temp2.put(SECOND_COLUMN,auditValues.get(countaudits).getName());
                    temp2.put(THIRD_COLUMN, auditValues.get(countaudits).getXcoordinate());             
                    temp2.put(FOURTH_COLUMN, auditValues.get(countaudits).getVMstartdate());

                    list.add(temp2);
                    countaudits++;
                }

        }   

        //////////////////////////////////////////////////////////////////////

        if(rb2.isChecked() == true)
        {
            assetsds = new AssetsDataSource(this);
            assetsds.open();

            List<Assets> assetValues = assetsds.getAllAssets();
            ArrayAdapter<Assets> assetadapter = new ArrayAdapter<Assets>(this,
                android.R.layout.simple_list_item_1, assetValues);

            int countassets = 0; 
            int size = assetValues.size();

            while (countassets < size)
            {
                HashMap temp = new HashMap();           

                temp.put(FIRST_COLUMN,assetValues.get(countassets).getAssetId());
                temp.put(SECOND_COLUMN,assetValues.get(countassets).getAssetDescription());
                temp.put(THIRD_COLUMN, assetValues.get(countassets).getInspectorId());
                temp.put(FOURTH_COLUMN,"insert time here");

                list.add(temp);
                countassets++;
            }
        }
        ListView lview = (ListView) findViewById(R.id.listView1);
        lview.invalidateViews();    
    }

public void insertConstant()
    {
        list.clear();
        HashMap temp0 = new HashMap();    

        temp0.put(FIRST_COLUMN, com.example.demoapplication1.Constant.FIRST_COLUMN);
        temp0.put(SECOND_COLUMN, com.example.demoapplication1.Constant.SECOND_COLUMN);
        temp0.put(THIRD_COLUMN,  com.example.demoapplication1.Constant.THIRD_COLUMN);
        temp0.put(FOURTH_COLUMN,  com.example.demoapplication1.Constant.FOURTH_COLUMN);

        list.add(temp0);
    }

如果您需要更多信息,请告诉我

【问题讨论】:

  • 每次更新列表视图时,都必须从列表适配器调用 notifyDataSetChanged()。

标签: java android user-interface android-listview


【解决方案1】:

您错过了在列表视图上设置适配器:

lview.setAdapter(assetadapter);

【讨论】:

  • 嗨,谢谢,这似乎有效。我的列表视图的格式有点疯狂,但我想主要问题已经解决了。谢谢
  • 好的。请验证我的回答(投票按钮下方的小复选标记)。
  • 不幸的是,我认为这是正确的答案,但 lview.setAdapter(assetadapter) 不能解决问题,因为我使用适配器的唯一时间是我将值放入临时 HashMap 时。然后自定义列表视图使用 hashmap 填充列表而不是适配器。所以这个问题还没有解决,请让我知道你的想法 - 我该如何解决这个问题 - 刷新列表必须与 hashmap 有关(我认为它确实填充但没有显示在列表视图中)。
  • 好的,但您没有告诉我们您使用自定义列表视图! :) 你能补充一下细节吗? (自定义列表视图,它是如何工作的,hashmap ?)
  • 是的,对不起,那是我的错。使用 4 个字段/列创建一个临时 hashmap 对象。在循环中,此对象获取 4 列信息并将其添加到列表中 (list.add(temp);) 添加所有记录后 - 这是它应该在列表中显示的位置。这在第一次创建时可以正常工作,但在我希望它刷新时却不行。当我在列表视图中删除所选记录时,这也可以正常工作 - 它会立即刷新并删除记录。与 (list.remove(id);)
【解决方案2】:

将适配器设置为列表视图

需要更新listview时需要调用Adapter的notifyDataSetChanged()

【讨论】:

  • 这行不通,因为我实际上并没有使用适配器,只使用它们的值
猜你喜欢
  • 2019-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-07
  • 2013-01-08
  • 2016-07-30
相关资源
最近更新 更多