Pooja Dubey Pooja Dubey - 5 months ago 512
Android Question

How to call a fragment from BaseAdapter Class Android?

I want to call a Fragement from my BaseAdapter Class. In this class I have button on click of which I want to call the new fragment, but I am not able to get this. I have to pass values from the click of the button to the fragment.

BaseAdapter Class

public class StatusAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public StatusAdapter(Activity a,
ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.approval_selftrip_inner, null);
TextView approved_by = (TextView) vi.findViewById(R.id.approved_by);
TextView status = (TextView) vi.findViewById(R.id.status);
TextView trip = (TextView) vi.findViewById(R.id.trip);
Button view_log = (Button)vi.findViewById(R.id.view_log);

HashMap<String, String> list = new HashMap<String, String>();
list = data.get(position);
approved_by.setText(list.get("first_id"));
status.setText(list.get("status"));
trip.setText(list.get("trip"));
view_log.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

//Here i want to call my fragment

}
});
return vi;
}
}


Fragement

public class Log extends Fragment {
Context context ;
View rootView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context = getActivity();

rootView = inflater.inflate(R.layout.activity_log,
container, false);
return rootView;
}


}


I want to call this Fragment from the BaseAdapter Class on click of
view_log
.Please help me how can we do this

After Martin Cazares I have done this

In Activity

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context = getActivity();
rootView = inflater.inflate(R.layout.activity_main,
container, false);
mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Recived",
Toast.LENGTH_LONG).show();
ApprovalLog fragment2 = new ApprovalLog();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment2);
fragmentTransaction.commit();
}
};
return rootView;
}


In The AdapterClass

view_approvallog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
CommonUtils.showAlert("Test in Adapter", activity);
activity.registerReceiver(mBroadcastReceiver, new IntentFilter(
"start.fragment.action"));

}
});

Answer

Simply use this.

public void onClick(View view) {
 LogFrag fragment2 = new LogFrag();
 FragmentManager fragmentManager = getFragmentManager();
 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
 fragmentTransaction.replace(R.id.fragment1, fragment2);
 fragmentTransaction.commit();
}
Comments