0

My ADAPTER File in these I declare the boolean value and one function i.e setCheckboxVisibilityStatus(), how to call that function in the activity. if I called like an adapter.setCheckboxVisibilityStatus() it shows error.

public class BrowseHuntSlideAdapter extends RecyclerView.Adapter {

public LayoutInflater inflater;
private ArrayList<BrowseHuntSlideModel> dataModelArrayList;
Double lat, lng;
String address;

private boolean hideChkbox = false;

public static ArrayList<Object> mySelectPlace = new ArrayList<Object> ();
public static ArrayList<String> mySelectPlaceAdd = new ArrayList<> ();
public static ArrayList<String> mySelectplaceImg = new ArrayList<> ();
public static ArrayList<String> mySelectplaceDesc = new ArrayList<> ();
public BrowseHuntSlideAdapter(Context ctx, ArrayList<BrowseHuntSlideModel> dataModelArrayList) {

    inflater = LayoutInflater.from ( ctx );
    this.dataModelArrayList = dataModelArrayList;
    //this.mapboxMap = mapBoxMap;

}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = inflater.inflate ( R.layout.browse_slide_list_item, parent, false );
    MyViewHolder holder = new MyViewHolder ( view );

    return holder;
}

public void setCheckboxVisibilityStatus(boolean status) {
    hideChkbox = status;

    notifyDataSetChanged();
}

@Override
public void onBindViewHolder(BrowseHuntSlideAdapter.MyViewHolder holder, int position) {


    if(hideChkbox)
        holder.checkBox.setVisibility(GONE); //Make it invisible instead of GONE if needed.
    else
        holder.checkBox.setVisibility(VISIBLE);

}

@Override
public int getItemCount() {
    return dataModelArrayList.size ();
}

class MyViewHolder extends RecyclerView.ViewHolder {

    TextView place_name, place_address, distance;
    ImageView place_image;
    Button button;
    CheckBox checkBox;

    public MyViewHolder(View itemView) {
        super ( itemView );
        Context context = itemView.getContext ();
        checkBox = (CheckBox)itemView.findViewById(R.id.checkbox);
        //place_address1 = (TextView) itemView.findViewById ( R.id.place_address );

    }

} }

and my Edit Button in Activity.......

public class Gotosafari extends AppCompatActivity implements OnMapReadyCallback, PermissionsListener {

String srno;
private RecyclerView rList, rList2;
private LinearLayoutManager linearLayoutManager;
private DividerItemDecoration dividerItemDecoration;
private static ArrayList<BrowseHuntSlideModel> PlacesList;
private RecyclerView.Adapter adapter, adapter1;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    rList = (RecyclerView) findViewById(R.id.recycle_slide_list);
    PlacesList = new ArrayList<>();
    adapter = new BrowseHuntSlideAdapter(getApplicationContext(),PlacesList);
    linearLayoutManager = new LinearLayoutManager(this); 
  linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    dividerItemDecoration = new 
    DividerItemDecoration(rList.getContext(), 
    linearLayoutManager.getOrientation());
    rList.setLayoutManager(new LinearLayoutManager(this, 
    LinearLayoutManager.VERTICAL, false));
    rList.addItemDecoration(dividerItemDecoration);
    rList.setAdapter(adapter);


    Button edtbtn = (Button) findViewById(R.id.edtbtn);
    Button donebtn = (Button) findViewById(R.id.donebtn);
    donebtn.setVisibility(View.INVISIBLE);

    edtbtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            edtbtn.setVisibility(View.INVISIBLE);
            donebtn.setVisibility(View.VISIBLE);
            edtbtn.setText("EDIT");
            adapter.setCheckBoxVisibilityStatus(true);
        }
    });

    donebtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            donebtn.setVisibility(View.INVISIBLE);
            edtbtn.setVisibility(View.VISIBLE);
            donebtn.setText("DONE");

            flagtrue= "false";
            adapter.notifyDataSetChanged();
            mySelectPlaceAdd.clear();
            mySelectplaceDesc.clear();
       }
    });
    addFirstStopToStopsList ();
}
Shubham
  • 23
  • 9
  • You can redirect on Activity through interface. Link: https://stackoverflow.com/questions/49969278/set-a-click-listener-to-a-recyclerview – mehul chauhan Jul 12 '19 at 13:34
  • Check my answer.. – Jaimin Modi Jul 12 '19 at 13:35
  • What exactly are you trying to achieve.. A select all functionality? in that case you should update your models and call notifydatasetchanged... If you still want your checkchange listener to be handled in activity.. create an interface and pass it to adapter so that you can call the callback... in kotlin you could even send a higer order function which is better.. – Kannan_SJD Jul 12 '19 at 13:44

4 Answers4

2

In your Adapter, Create an Interface like this :

public interface ListAction {
    public void onCheckBoxClick(int position);
}

Then, Take Click of your Checkbox as below :

        holder.your_check_box.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    listAction.onCheckBoxClick(position);
                }
            }
        });

Then, In your Activity implement that Interface and override it's method :

class YourActivity implements YouAdapterClass.ListAction

and override it's method as below :

    @Override
    public void onCheckBoxClick(int position){
    //Do Whatever you want here..
    }

Simple..!!

EDIT

Also pass listener from your Activity to Adapter class through Constructor as below :

mAdapter = new YouAdapterClass(getActivity(),this);

this - second argument.

And In your adapter class do as below :

//Declare

private YourAdapterClass.ListAction listAction;

//Constructor

public YourAdapterClass(Context mContext, YourAdapterClass.ListAction listAction) {
        this.mContext = mContext;
        this.listAction = listAction;
    }
Jaimin Modi
  • 1,530
  • 4
  • 20
  • 72
  • Thank you. but I just want to set visibility of a checkbox on button click and my button is in activity and checkbox is in Recycler view list means in an adapter. so how can i set it from activity? – Shubham Jul 12 '19 at 13:46
  • Just use Model Class bro. Take one seperate variable in your model class and store Boolean value in it. If it's true set Visible else set Invisible. Check it on click of checkbox and then notify your adapter in above method which in activity (overridden) - yourAdapter.notifyDataSetChanged(). – Jaimin Modi Jul 12 '19 at 13:48
  • i have one edit button in my activity. and checkbox are in my recyclerview list means in an adapter. when i click on edit button all checkbox is set to visible. so plz tell me how to di it? – Shubham Jul 12 '19 at 13:56
  • Have you created a Model Class ? If Yes then pls. post it in your question. I will guide If it still pending. – Jaimin Modi Jul 15 '19 at 06:28
0

Create a method in recycler view adapter like public void hideCheckBox(); In that method get that checkbox view by tag and hide it. You must add a tag to the checkbox which you want to hide. You can call that method from activity as you have the adapter instance.

For example,

In your adapter, a method is like,

public void hideCheckBox(){
  //get view by tag and hide it
  notifyDataSetChanged();
}

Now suppose your adapter name is mAdapter then,

 edtbtn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        edtbtn.setVisibility(View.INVISIBLE);
        donebtn.setVisibility(View.VISIBLE);
        edtbtn.setText("EDIT");

   // want to apply code here.
     mAdapter.hideCheckBox();
    }
});
niks
  • 173
  • 10
  • how to call that method in activity. and if u have any reference then please let me know – Shubham Jul 12 '19 at 13:44
  • check my edit hope that will help. If not let me know. – niks Jul 12 '19 at 13:59
  • i have one edit button in my activity. and checkbox are in my recyclerview list means in an adapter. when i click on edit button all checkbox is set to visible. so plz tell me how to di it? – Shubham Jul 12 '19 at 14:10
  • Correct me if I am wrong- So in recyclerview only checkboxes are present and all should be visible or invisible when user click edit button(which is in activity) right? – niks Jul 12 '19 at 14:16
  • it shows error to call method in activity. how to call adapter method in activity – Shubham Jul 13 '19 at 06:16
  • `adapter.hideCheckBox();` here it shows error it does not accept that method in activity – Shubham Jul 13 '19 at 10:10
  • there should not be any problem calling a public method from activity... have you defined the method as public? also tell me is your Adapter in the same activity from where you want to call? – niks Jul 13 '19 at 10:26
  • no my adapter file is different. and yes method is public – Shubham Jul 13 '19 at 10:32
  • Then It should work bro. I don't get exactly what is the issue with calling that method. I did that several times and it worked. – niks Jul 13 '19 at 10:46
  • I will suggest you to Share more code so that we can solve your query. – niks Jul 13 '19 at 10:54
  • sure give me 2min – Shubham Jul 13 '19 at 10:55
  • It should work. Can you share the activity code too? – niks Jul 13 '19 at 11:10
  • adapter = new BrowseHuntSlideAdapter(getApplicationContext(),PlacesList); dont pass application context pass the activity context and change - private RecyclerView.Adapter adapter, adapter1; to private BrowseHuntSlideAdapter adapter, adapter1; it will work – niks Jul 13 '19 at 11:41
0

When you say hide... Do you want to hide all the checkbox at once...? If yes, then create a boolean flag in your adapter and set it to true initially. Then create a setter method, where you can set it to false and notifyDataSetChanged later from activity... in onBindViewHolder method you should set the visibility of the checkbox comparing that flag..

If you want to hide certain checkbox in the list and not all of them.. You should have that flag in the data model and then change it later on and call notifyItemChanged to those particular items...

Kannan_SJD
  • 1,022
  • 2
  • 13
  • 32
0

Take a flag variable in your adapter class as class variable.

private int boolean hideChkbox = false; //false intially so that it will show open adapter created.

Now create a method in the adapter.

public void setCheckboxVisibilityStatus(boolean status) {
    hideChkbox = status;

    notifyDataSetChanged();
}

Now what you need to do is that add condition in your onBindViewHolder() method to make the checkbox visisble or hide. So the code snippet will be-

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    if(hideChkbox)
        holder.chkbox.setVisibility(GONE) //Make it invisible instead of GONE if needed.
    else
        holder.chkbox.setVisibility(VISIBLE)

    //Note don't forget to put both if-else condition above otherwise irrelevant result may be seen while scrolling.

    ..... add your rest of code logic here
}

Next you need to call this method from your activity when you want to change the visibility in your adapter class. If need to hide then pass true as method parameter else false. So the calling from Activity will be-

edtbtn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        edtbtn.setVisibility(View.INVISIBLE);
        donebtn.setVisibility(View.VISIBLE);
        edtbtn.setText("EDIT");

        // want to apply code here.
        adapter.setCheckboxVisibilityStatus(true);
    }
});

Hope it will solve the issue.

Hari N Jha
  • 484
  • 1
  • 3
  • 11