Need expert opinion how should i structure this issue. I have a custom method process_filter that resides in a fragment as it needs to access a private TextView
and List
of this fragment.
In the middle of processing, this fragment will access a BaseAdapter
and inside this BaseAdapter
I need to use back process_filter method
Basically here is the structure:
MyFragment.java
public class MyFragment extends Fragment {
private List<String> filter_list;
private TextView no_of_filter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
no_of_filter = (TextView) view.findViewById(R.id.no_of_filter_tv);
.
MyAdapter custom_adapter = new MyAdapter(context, "string 1", "string 2");
.
process_filter("string 1", "string 2");
.
}
public void process_filter(String in_preference, String current_value)
{
no_of_filter.setText(in_preference);
}
MyAdapter.java
class MyAdapter extends BaseAdapter {
public View getView( final int position, View convertView, ViewGroup parent)
{
holder.checkBox.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
//Here I need to access back process_filter from fragment
process_filter ("string 1, string 2");
}
}
}
}
Pass the instance to fragment to your custom adapter through constructor. the way you are calling onSuccess() inside onClick() method is wrong... It should be called on fragment as it is Fragment's method.
To pass data between fragments in the same fragment manager, the listener should be added to the destination fragment with requestKey in order to receive the result produces from another fragment with the same key.
You need to call an extra method every time you want to instantiate the Fragment . You can't guarantee that mListener is set at any time. You may need to pepper your Fragment code with null checks. You need to be careful to make sure the listener remains set after lifecycle events such as screen rotation.
This example demonstrates how to call an activity method from a fragment in an Android App using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Create an interface from your adapter to your fragment.
In your adapter create the interface and pass it in your adapter's constructor
class MyAdapter extends BaseAdapter {
public interface IProcessFilter {
void onProcessFilter(String string1, String string2)
}
private IProcessFilter mCallback;
public MyAdapter(Context context, String string1, String string2, IProcessFilter callback) {
mCallback = callback;
}
public View getView( final int position, View convertView, ViewGroup parent)
{
holder.checkBox.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
mCallback.onProcessFilter("string1", "string2");
}
}
}
}
Last thing, implement it in your fragment like this
public class MyFragment extends Fragment implements IProcessFilter {
...
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
no_of_filter = (TextView) view.findViewById(R.id.no_of_filter_tv);
MyAdapter custom_adapter = new MyAdapter(context, "string 1", "string 2", this);
}
@Override
public void onProcessFilter(String string1, String string2) {
// Process the filter
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With