如何在SherlockFragment中刷新listView?

时间:2013-09-03 22:23:09

标签: android listview android-listview android-fragments

我在SherlockFragment中有一个listView,但是当我对适配器进行任何更改时,我无法在屏幕上更新listView。我试过:adapter.notifyDataSetChanged(),但没有成功。感谢的。

2 个答案:

答案 0 :(得分:1)

在调用notifyDatasetChanged()之前,您应该更新传递给适配器的基础数据集;

EG:

对于ListActivity中的ArrayAdapter (“arraylist”是您用来支持ArrayAdapter的ArrayList)

arraylist.add(data);
arrayadapter = this.getListAdapter();
arrayadapter.notifyDatasetChanged();

答案 1 :(得分:0)

就个人而言,每次按下刷新按钮的用户都会重新填充listView,再次初始化Cursor

像这样:调用此函数......

public void repopulateListView(){

cursor = dbHelper.fetchAll();
        columns = new String[] {                        
                DBAdapter.KEY_NAME,
                DBAdapter.KEY_DATE,
                DBAdapter.KEY_VOTE,
                DBAdapter.KEY_CREDIT
        };
        to = new int[] {
                R.id.one,
                R.id.two,
                R.id.three,
                R.id.four
        };

        dataAdapter = new SimpleCursorAdapter(
                getActivity(), R.layout.YOUR_ID,
                cursor,
                columns,
                to,
                0)
        {
            @Override
            public View getView(int position, View convertView, ViewGroup parent)
            {
                final View row = super.getView(position, convertView, parent);

            }
        }
        }

...来自Refresh onClick:

 @Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.refresh:{
            repopulateListView();
            break;
        }
    }
 }