1

我制作了一个列表视图,数据是从 sqlite 获取的,现在我想让它使用单选按钮按名称或位置排序

这是我的代码

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        db = new DatabaseHelper(this);
        RadioButton rbname = (RadioButton)findViewById(R.id.radio0);
                RadioButton rblocation = (RadioButton)findViewById(R.id.radio1);

            if (rbname.isChecked()){
            Cursor = db
                .getReadableDatabase()
                .rawQuery("SELECT *  FROM " + DatabaseHelper.LIST + " ORDER BY " + DatabaseHelper.NAME, null);  
            ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, Cursor,
                new String[] {DatabaseHelper.NAME, DatabaseHelper.ADDRESS, DatabaseHelper.GENDER, DatabaseHelper.LOCATION},
                new int[] {R.id.txtName, R.id.txtAddress, R.id.txtGender, R.id.txtLocation});       
            setListAdapter(adapter);

            }

            else if (rblocation.isChecked()){
                Cursor = db
                        .getReadableDatabase()
                        .rawQuery("SELECT *  FROM " + DatabaseHelper.LIST+ " ORDER BY " + DatabaseHelper.LOCATION, null);   
                ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, Cursor,
                        new String[] {DatabaseHelper.NAME, DatabaseHelper.ADDRESS, DatabaseHelper.GENDER, DatabaseHelper.LOCATION},
                        new int[] {R.id.txtName, R.id.txtAddress, R.id.txtGender, R.id.txtLocation});       
                    setListAdapter(adapter);
            }


        registerForContextMenu(getListView());
    }

我已经尝试将Cursor.requery()已经尝试将 ListAdapter 更改为 BaseAdapter 以便我可以使用notifydatasetchanged()

但什么也没发生..我的列表视图仍然没有更新

请帮我解决这个问题

4

2 回答 2

1

您需要做的是将您的代码放入侦听器中,以便您的事件在单击按钮后激活,而不是在onCreate. 此外,通过重新实例化游标并调用changeCursor适配器来获取由游标支持的列表视图以进行更改。我希望下面的代码能让事情更清楚:

private RadioButton rbname, rblocation;
private SimpleCursorAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    db = new DatabaseHelper(this);
    rbname = (RadioButton)findViewById(R.id.radio0);
    rblocation = (RadioButton)findViewById(R.id.radio1);

    Cursor cursor = db
            .getReadableDatabase()
            .rawQuery("SELECT *  FROM " + DatabaseHelper.LIST + " ORDER BY " + DatabaseHelper.NAME, null);
    // a default cursor
    adapter = new SimpleCursorAdapter(this, R.layout.row, Cursor,
            new String[] {DatabaseHelper.NAME, DatabaseHelper.ADDRESS, DatabaseHelper.GENDER, DatabaseHelper.LOCATION},
            new int[] {R.id.txtName, R.id.txtAddress, R.id.txtGender, R.id.txtLocation});       
    setListAdapter(adapter);    // do the adapter stuff here to avoid repeating code
    initListeners(); // set up the listeners for the radio buttons

    registerForContextMenu(getListView());
}

private void initListeners() {

    rbname.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                Cursor cursor = db
                        .getReadableDatabase()
                        .rawQuery("SELECT *  FROM " + DatabaseHelper.LIST + " ORDER BY " + DatabaseHelper.NAME, null);
                adapter.changeCursor(cursor); // change cursor in adapter so that listview changes                  
            }               
        }
    });

    rblocation.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                Cursor cursor = db
                        .getReadableDatabase()
                        .rawQuery("SELECT *  FROM " + DatabaseHelper.LIST+ " ORDER BY " + DatabaseHelper.LOCATION, null);   
                adapter.changeCursor(cursor);   
            }               
        }
    });
}
于 2012-11-25T09:37:19.593 回答
1

很久以前我有同样的问题,帮助生成刷新方法。所以每次我需要刷新时,我都会简单地调用这个方法。

         private void refresh() {
           ListView yourListView = getListView();   
           list = getList(); //this is a List<string>
           adapter = new ArrayAdapter<String> (this,R.layout.your_listview_item,list); //this is ArrayAdapter<String>       
           yourListView.setAdapter(adapter);
           yourListView.invalidateViews();
}

这在我的 ListActivity 中效果很好

于 2012-11-25T07:58:27.473 回答