2016-09-26 143 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

要更清楚地向我展示您的自定義視圖課程,以便我可以將其引用 –