在Android中刷新/重新加载CustomView

时间:2016-09-26 17:35:21

标签: java android android-custom-view custom-view

我有一个CustomView,在其View上显示一个数字,这个数字来自数据库。首先工作正常,当我移动到另一个Activity,更改数据库数量并调用finish();方法时,第一个Activity将出现,我想刷新或重新加载在第一个加载中初始化的CustomView以显示它的更新价值。怎么可能?

这是我的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);

}

我在近15个活动中使用它,我想刷新它

2 个答案:

答案 0 :(得分:1)

您可以通过调用活动的onResume()方法或onActivityResult()方法

来从数据库中获取值并更新其值并分配到自定义视图
package com.dialogdemo;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

public class Navigation extends LinearLayout {

    PersianTextView workorder_count;
    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);

         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));
            }
        });

        refreshView();

    }

    public void refreshView(){
        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);
    }
}

refreshView()中调用onResume()方法中的所有活动

答案 1 :(得分:0)

我在CustomView中添加了一个名为pip install pypiwin32 的函数,并从第一个Activity的update();方法中调用它,如下所示:

onResume