2016-09-26 86 views
-2

我有一个CustomView,它在View上显示一个数字,这个数字来自数据库。当我移动到另一个Activity时,更改数据库数量并调用finish();方法时,第一个Activity将显示,并且我想刷新或重新加载在第一次加载中初始化以显示其更新值的CustomView。这怎么可能?在Android中刷新/重新加载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的活动,我想刷新

+0

如何显示在第一View中的数地点?这应该是完全相同的。 –

+0

另外,你如何开始第二个活动?你正在使用startActivity()或startActivityForResult()吗? –

+0

我通过staratActivity()方法开始它 –

回答

0

我加在我的CustomView称为update();功能,从第一个活动的onResume方法是这样称呼它:

((CustomView) findViewById(R.id.customview)).update(); 
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()在你所有的活动,将工作

+0

我想调用onResume上的CustomView代码,但它们只会在setContentView方法中调用! –

+0

好的,那么在这种情况下,您必须在第一时间找到该视图的ID,之后您将使用这些ID进一步更新目的 –

+0

要更清楚地向我展示您的自定义视图课程,以便我可以将其引用 –