如何将 CustomListAdapter 连接到 Activity 中的列表片段?

How do you connect a CustomListAdapter to a List Fragment in the Activity?

上下文是:

  1. 我有一个 activity 由一个片段组成,其中有一个表格可以输入日期和航班号。那里没有其他片段。
  2. 单击按钮时,会向网络服务发出请求。
  3. Web 服务返回了 List 个飞行对象。
  4. 为 listFragment 中的每一行创建自定义 ArrayAdapter
  5. 在前端的相同 Activity 的 ListFragment 中显示结果。

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;
    }
}

注意:ListFragment 有一个由单个列表视图组成的默认布局。 Source

所以我不需要列表的布局文件。

MyActivity.java: 对象列表 (flightList.getFlight) 用于创建 CustomListAdapter。此代码位于 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();
    }
}

所以我没有得到的是如何在 MyActivity 上显示 FlightFragment。 此外如何连接它以便显示结果:我认为使用 setListAdapter?

更新:将片段连接到 Activity 1.在activity布局中添加一个FrameLayout,或者其他fragment布局

<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。使用 FragmentManager 以编程方式调用片段:

//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();
        }

因此正在调用片段的 OnCreate 方法,但列表为空,因为我没有将 Flight 对象传递给片段。我该怎么做?

How can I pass the List object to the Fragment?

因为 List 包含 Flight 自定义 class 对象。所以直接pass是不行的。

传递List<Flight>对象:

1.Flight class.

中实施 Serializable

2. 使用 Bundle.putSerializable 将对象从 Activity 发送到 FlightFragment :

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

3.FlightFragment class 覆盖onCreateView 方法和getArguments 方法:

@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);
 }