How do I get notifyDatasetChanged() to work with a ListAdapter?

前端 未结 2 2058
醉话见心
醉话见心 2021-01-13 01:27

Right now I use setAdapter to update my ListView, but I think the proper way is to use notifiyDatasetChanged() and I can\'t get that to work in my main class (it\'s in the a

相关标签:
2条回答
  • 2021-01-13 01:38

    Dont call the notifyDataSetChanged(); method while creation.

    only call it when content of your listViewScore changes.. and to use it at that time-

    replace

    listView.getAdapter().notifyDatasetChanged();
    

    with

    ((ScoreListAdapter)listView.getAdapter()).notifyDataSetChanged();
    

    and see the magic...

    thanks.

    0 讨论(0)
  • 2021-01-13 01:50

    Create an instance of your custom adapter, so you can use it anywhere you like...

    public class ScoreList extends SherlockFragmentActivity {
    
    private ListView listViewScore;
    
    private ScoreListAdapter adapter;
    
    static List<Score> listScore = new ArrayList<Score>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.score_list);
        ctx = this;
        listScore = dbh.getAllScores();
    
        listViewScore = (ListView) findViewById(R.id.score_list);
    
        adapter = new ScoreListAdapter(ctx, R.layout.score_row_item, listScore);
        listViewScore.setAdapter(adapter);
        adapter.notifyDatasetChanged(); 
    }
    }
    

    By the way, if your listScore array is already loaded, then you do not need to use

    adapter.notifyDatasetChanged(); 
    
    0 讨论(0)
提交回复
热议问题