Search code examples
javaandroidandroid-custom-viewcustom-view

Refresh/Reload CustomView in Android


I have a CustomView that shows a number on its View and this digit came from the database. that works fine in the first place, when I move to another Activity, change the database amount and call finish(); method, the first Activity will Appear and I want to refresh or reload the CustomView that initialized in the first load to show its updated value. how is it possible?

here is my CustomClass:

public class Navigation extends LinearLayout {


public Navigation(Context context) {
    super(context);
    this.initComponent(context);
}

public Navigation(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.initComponent(context);
}

private void initComponent(Context context) {

    LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(R.layout.navigation_bar, null, false);
    this.setOrientation(VERTICAL);
    this.addView(v);

    LinearLayout workorder = (LinearLayout) v.findViewById(R.id.workorder);

    PersianTextView workorder_count = (PersianTextView) v.findViewById(R.id.workorder_count);

    workorder.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            getContext().startActivity(new Intent(getContext(),Workorders.class));
        }
    });

    database db = new database(getContext());
    db.open();
    Cursor cu = db.Display_shared("SELECT * FROM [Workorder_Repair] WHERE [Workorder_Repair_For_Department_ID] = "+ Login.dep_id+" AND [Workorder_Repair_Status] = 1");

    if(cu.getCount()>0)
        workorder_count.setText(""+cu.getCount());
    else
        workorder_count.setVisibility(INVISIBLE);

}

I use it in almost 15 activities and I want to refresh it


Solution

  • I add a function called update(); in my CustomView and call it from first Activity's onResume method like this:

    ((CustomView) findViewById(R.id.customview)).update();