Refresh/Reload Android 中的自定义视图
Refresh/Reload CustomView in Android
我有一个 CustomView,它在其 View 上显示一个数字,这个数字来自数据库。首先工作正常,当我移动到另一个 Activity,更改数据库数量并调用 finish();
方法时,第一个 Activity 将出现,我想刷新或重新加载 CustomView在第一次加载时初始化以显示其更新值。怎么可能?
这是我的自定义类:
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 个活动中使用它,我想刷新它
您可以通过调用 activity 的 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);
}
}
在您的所有活动中调用 onResume()
中的 refreshView()
方法
我在我的 CustomView 中添加了一个名为 update();
的函数,并从第一个 Activity 的 onResume
方法调用它,如下所示:
((CustomView) findViewById(R.id.customview)).update();
我有一个 CustomView,它在其 View 上显示一个数字,这个数字来自数据库。首先工作正常,当我移动到另一个 Activity,更改数据库数量并调用 finish();
方法时,第一个 Activity 将出现,我想刷新或重新加载 CustomView在第一次加载时初始化以显示其更新值。怎么可能?
这是我的自定义类:
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 个活动中使用它,我想刷新它
您可以通过调用 activity 的 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);
}
}
在您的所有活动中调用 onResume()
中的 refreshView()
方法
我在我的 CustomView 中添加了一个名为 update();
的函数,并从第一个 Activity 的 onResume
方法调用它,如下所示:
((CustomView) findViewById(R.id.customview)).update();