Search code examples
androidadapterbaseadapter

Which activity called the adapter


I am using an adapter to populate a listview , I am using same adapter in 2 activities but I need to hide an element of the layout in one case and not in another . So is there a way to figure out which activity called the adapter (in the adapter 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;
}

Solution

  • So is there a way to figure out which activity called the adapter (in the adapter class )

    Yes of course. Simple thing is to send an additional parameter from the Adapter constructor and find out which class calls it. Create a String field.

    String className;
    

    And in the constructor:

        public Db_adapter(Context mContext, List<db_list> mDataList, String className) {
        //make a field and assign it with className
    this.className = className
        }
    

    And while either showing or hiding,

    if(className.equals("ClassA")){
    //hide something
    yourTextView.setVisibility(View.GONE);
    }else if(className.equals("ClassB")){
    //show something
    yourTextView.setVisibility(View.VISIBLE);
    }