How to call ListView adapter from another activity for refresh - notifyDataSetChanged()

不打扰是莪最后的温柔 提交于 2019-12-24 17:24:05

问题


I want to call a listview adapter from another activies after insert record to sqllite db.

I cant call the adapter.notifyDataSetChanged() method. What is the best way to do it.

public void onClick(View v) {
    TextView item_name;
    item_name=(TextView) findViewById(R.id.eItemName);         
    Log.d("Insert: ", "Inserting ..");
    db.add_item(new Item(item_name.getText().toString()), getApplicationContext());

    // I want to call the method HERE!!!! 

    finish();   
}

回答1:


Just

After finish() Current Activity, Call adapter.notifyDataSetChanged() from onActivityResult()in previous Activity (As I assumes your Listview in this previous Activity).

Also start your Current Activity from Previous Activity using startActivityForResult();

Or if you have reference of List Adapter in this Activity, then using that reference call adapter.notifyDataSetChanged(). (But I think its not a good practice)




回答2:


The better option would be re-loading your ListView with new data inside onResume() of your Activity where you have ListView. There is no need to call notifyDataSetChanged() from another class as you don't have your List visible in your another Activity.

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

    //fill your Collection with new data
    // setAdapter or notify your adapter
}


来源:https://stackoverflow.com/questions/11591729/how-to-call-listview-adapter-from-another-activity-for-refresh-notifydatasetch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!