简体   繁体   中英

Android - Update ListView not Working

I am trying to update a ListView at Android. I insert new items on the list and then call notifyDataSetChanged() and nothing happens! And I am using the runOnUIThread, that should work right?

Here is my code:

    @Override

//this is where I create the ListView with my custom adapter..

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Globals.getInstance().readWarehouse(this);
            Toast.makeText(this, "É:" + Globals.getInstance().getWarehouse().getJointNames().length , Toast.LENGTH_LONG).show();
            setContentView(R.layout.activity_main);
            adapter = new CustomList(MainActivity.this, Globals.getInstance().getWarehouse());
            list = (ListView) findViewById(R.id.list);
            list.setAdapter(adapter);

    }

Then I edit the content of this adaptar on another activity.. adding nore more item.. And I come back to this activity and then I call this method to refresh the list.. but nothing is happenning.. Here is the code where I refresh:

@Override
    protected void onResume(){
        super.onResume();

        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                ((BaseAdapter) list.getAdapter()).notifyDataSetChanged();
            }
        });

        Toast.makeText(this, "onResume triggered.. and list not refreshing.. even after the call made before to notify.." , Toast.LENGTH_LONG).show();

    }

Can someone help me? Thanks alot!

I solved the problem by creating the layout myself dynamically adding new rows and stuff like that.. using the Layout Inflater.

It's not the best solution, but know I have full control on the layout :)

Thanks anyway ;)

Check if adapter (CustomList) getView function is called after onResume . If getView function is called, check if data is updated in Globals.getInstance().getWarehouse() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM