0

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

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

这是我的代码:

在 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);
    }

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

4

2 回答 2

1

您错过了在 listview 上设置适配器:

lview.setAdapter(assetadapter);
于 2013-08-28T13:57:09.973 回答
0

将适配器设置为列表视图

当需要更新列表视图时,需要调用 Adapter 的 notifyDataSetChanged()

于 2013-08-28T14:01:31.053 回答