0

The context is:

  1. I have an activity consisting of a fragment where there is a form to enter a date and a flight number. There are no other fragments there.
  2. On click of a button, a request is made to a webservice.
  3. A List of Flight Objects is returned from the webservice.
  4. A custom ArrayAdapter is created for each row in the listFragment.
  5. Show the results in a ListFragment in the same Activity on the frontend.

FlightResultAdapter.java:

public class FlightResultAdapter extends ArrayAdapter<Flight>{
    public FlightResultAdapter(Context context, List<Flight> flights) {
        super(context, R.layout.item_flight, flights);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Flight flight = getItem(position);

        if (convertView == null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_flight, parent, false);
        }
//Set the content of the custom row layout
        TextView tvFlightNum = (TextView) convertView.findViewById(R.id.tvFlightNum);
        tvFlightNum.setText(flight.getNumber());

        return convertView;
    }
}

Note: ListFragment has a default layout that consists of a single list view. Source

So I do not need a layout file for the list.

MyActivity.java: A list of objects (flightList.getFlight) is used to create the CustomListAdapter. This code is inside a function that is triggered on an OnClick.

FlightResultAdapter adapter = 
new FlightResultAdapter(getApplicationContext(), flightList.getFlight());

FlightFragment.java

public class FlightFragment extends ListFragment {

    private List<Flight> flight_items;

    public FlightFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        flight_items = new ArrayList<Flight>();

        //Initialise the list adapter and set
        FlightResultAdapter adapter = new FlightResultAdapter(getActivity(), flight_items);
        setListAdapter(adapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        Flight flight = flight_items.get(position);

        Toast.makeText(getActivity(), flight.getNumber(), Toast.LENGTH_LONG).show();
    }
}

So what I'm not getting is how to Show the FlightFragment on MyActivity. Furthermore how to connect it so results are displayed: I think setListAdapter is used?

Update: Connecting the Fragment to the Activity 1. Add a FrameLayout to the activity layout, or other fragment layout

<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

2. Use the FragmentManager to call the Fragment programmatically:

//Create the adapter here
        FlightResultAdapter adapter = new FlightResultAdapter(getApplicationContext(), flightList.getFlight());

        //Get the Fragment here and set the ListAdapter
        FlightFragment ff = new FlightFragment();
        // In case this activity was started with special instructions from an
        // Intent, pass the Intent's extras to the fragment as arguments
        ff.setArguments(getIntent().getExtras());

        ff.setListAdapter(adapter);

        if (findViewById(R.id.fragment_container) != null) {

            //Send the Data to the fragment - Need Help Here
            Bundle bundle = new Bundle();

            //Show the Fragment
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction =
                    fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.fragment_container, ff);
            fragmentTransaction.commit();
        }

So the Fragment's OnCreate Method is being called, but the list is empty because I am not passing the Flight Object to the Fragment. How can I do this?

tread
  • 10,133
  • 17
  • 95
  • 170

1 Answers1

1

How can I pass the List object to the Fragment?

Because List contains Flight custom class object. so it's not possible to pass it directly.

To pass List<Flight> object:

1. Implement Serializable in Flight class.

2. Use Bundle.putSerializable for sending object from Activity to FlightFragment :

Bundle bundle = getIntent().getExtras();  
bundle.putSerializable("flightList", flightList.getFlight());
ff.setArguments(bundle);
...

3. In FlightFragment class override onCreateView method and getArguments method:

@Override
 public View onCreateView(LayoutInflater inflater, 
            ViewGroup container, Bundle  savedInstanceState) {
  Bundle args = getArguments();
  List<Flight> flightList=(List<Flight>)args.getSerializable("flightList");
....
 return super.onCreateView(inflater, container, savedInstanceState);
 }
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213