哪个activity调用了适配器
Which activity called the adapter
我正在使用适配器来填充列表视图,
我在 2 个活动中使用相同的适配器,但我需要在一种情况下而不是在另一种情况下隐藏布局的元素。
那么有没有办法找出哪个 activity 调用了适配器(在适配器 class 中)?
public class Db_adapter extends BaseAdapter {
private Context mContext;
private List<db_list> mDataList;
private LayoutInflater mLayoutInflater;
private String TAG = this.getClass().getSimpleName();
public Db_adapter(Context mContext, List<db_list> mDataList) {
this.mContext = mContext;
this.mDataList = mDataList;
this.mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public int getViewTypeCount() {
return super.getViewTypeCount();
}
@Override
public int getCount() {
return mDataList.size();
}
@Override
public Object getItem(int position ) {
return mDataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = mLayoutInflater.inflate(R.layout.dblayout,parent,false);
viewHolder.tv_db_name= (TextView) convertView.findViewById(R.id.tv_db_name);
viewHolder.tv_db_number = (TextView) convertView.findViewById(R.id.tv_db_number);
viewHolder.iv_delete = (ImageView)convertView.findViewById(R.id.iv_delete);
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) convertView.getTag();
}
db_list rowData = mDataList.get(position);
viewHolder.tv_db_name.setText(rowData.getDb_name());
viewHolder.tv_db_number.setText(rowData.getDn_number());
viewHolder.iv_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new DeleteDb_async().execute(Add_deliveryBoy.db_id_list.get(position), String.valueOf(position));
}
});
return convertView;
}
public void onDataSetChanged(List<db_list> mDataList) {
this.mDataList = mDataList;
notifyDataSetChanged();
}
private class ViewHolder{
private TextView tv_db_name,tv_db_number;
private ImageView iv_delete;
}
So is there a way to figure out which activity called the adapter (in
the adapter class )
当然可以。简单的事情是从 Adapter 构造函数发送一个附加参数并找出哪个 class 调用它。创建一个字符串字段。
String className;
在构造函数中:
public Db_adapter(Context mContext, List<db_list> mDataList, String className) {
//make a field and assign it with className
this.className = className
}
并且在显示或隐藏时,
if(className.equals("ClassA")){
//hide something
yourTextView.setVisibility(View.GONE);
}else if(className.equals("ClassB")){
//show something
yourTextView.setVisibility(View.VISIBLE);
}
您可以使用mContext。
Adapter 被实例化的时间是有价值的。
是 Activity 创建了它。
因此,您可以使用任何您喜欢的方法,让 Activity 对象触手可及。
您可以创建另一个构造函数,再添加一个 boolean
值并将其存储在适配器中的变量中,如下所示:
boolean flag;
public Db_adapter(Context mContext, List<db_list> mDataList,boolean anotherAcivityFlag) {
this.mContext = mContext;
this.mDataList = mDataList;
flag = anotherAcivityFlag;
this.mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public Db_adapter(Context mContext, List<db_list> mDataList) {
this.mContext = mContext;
this.mDataList = mDataList;
flag = false;
this.mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
在 getView() 中添加一个 if 语句来检测您要加载的视图。
现在用构造函数初始化适配器以区分 bw 活动。
正如您所说,让我们假设我有两个活动 ActivityOne 和 ActivityTwo
Context context;
List<db_list> mDataList;
public Db_adapter(Context context, List<db_list> mDataList) {
this.context = context;
this.mDataList = mDataList;
}
在你的方法中你可以这样做
if(context instanceof ActivityOne) {
// setVisibility
} else if(context instanceof ActivityTwo) {
// setVisibility
}
我正在使用适配器来填充列表视图, 我在 2 个活动中使用相同的适配器,但我需要在一种情况下而不是在另一种情况下隐藏布局的元素。 那么有没有办法找出哪个 activity 调用了适配器(在适配器 class 中)?
public class Db_adapter extends BaseAdapter {
private Context mContext;
private List<db_list> mDataList;
private LayoutInflater mLayoutInflater;
private String TAG = this.getClass().getSimpleName();
public Db_adapter(Context mContext, List<db_list> mDataList) {
this.mContext = mContext;
this.mDataList = mDataList;
this.mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public int getViewTypeCount() {
return super.getViewTypeCount();
}
@Override
public int getCount() {
return mDataList.size();
}
@Override
public Object getItem(int position ) {
return mDataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = mLayoutInflater.inflate(R.layout.dblayout,parent,false);
viewHolder.tv_db_name= (TextView) convertView.findViewById(R.id.tv_db_name);
viewHolder.tv_db_number = (TextView) convertView.findViewById(R.id.tv_db_number);
viewHolder.iv_delete = (ImageView)convertView.findViewById(R.id.iv_delete);
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) convertView.getTag();
}
db_list rowData = mDataList.get(position);
viewHolder.tv_db_name.setText(rowData.getDb_name());
viewHolder.tv_db_number.setText(rowData.getDn_number());
viewHolder.iv_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new DeleteDb_async().execute(Add_deliveryBoy.db_id_list.get(position), String.valueOf(position));
}
});
return convertView;
}
public void onDataSetChanged(List<db_list> mDataList) {
this.mDataList = mDataList;
notifyDataSetChanged();
}
private class ViewHolder{
private TextView tv_db_name,tv_db_number;
private ImageView iv_delete;
}
So is there a way to figure out which activity called the adapter (in the adapter class )
当然可以。简单的事情是从 Adapter 构造函数发送一个附加参数并找出哪个 class 调用它。创建一个字符串字段。
String className;
在构造函数中:
public Db_adapter(Context mContext, List<db_list> mDataList, String className) {
//make a field and assign it with className
this.className = className
}
并且在显示或隐藏时,
if(className.equals("ClassA")){
//hide something
yourTextView.setVisibility(View.GONE);
}else if(className.equals("ClassB")){
//show something
yourTextView.setVisibility(View.VISIBLE);
}
您可以使用mContext。 Adapter 被实例化的时间是有价值的。 是 Activity 创建了它。 因此,您可以使用任何您喜欢的方法,让 Activity 对象触手可及。
您可以创建另一个构造函数,再添加一个 boolean
值并将其存储在适配器中的变量中,如下所示:
boolean flag;
public Db_adapter(Context mContext, List<db_list> mDataList,boolean anotherAcivityFlag) {
this.mContext = mContext;
this.mDataList = mDataList;
flag = anotherAcivityFlag;
this.mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public Db_adapter(Context mContext, List<db_list> mDataList) {
this.mContext = mContext;
this.mDataList = mDataList;
flag = false;
this.mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
在 getView() 中添加一个 if 语句来检测您要加载的视图。
现在用构造函数初始化适配器以区分 bw 活动。
正如您所说,让我们假设我有两个活动 ActivityOne 和 ActivityTwo
Context context;
List<db_list> mDataList;
public Db_adapter(Context context, List<db_list> mDataList) {
this.context = context;
this.mDataList = mDataList;
}
在你的方法中你可以这样做
if(context instanceof ActivityOne) {
// setVisibility
} else if(context instanceof ActivityTwo) {
// setVisibility
}