上下文是:如何將CustomListAdapter連接到Activity中的List Fragment?
- 我具有由其中存在的形式輸入一個日期和一個航班號的片段的活性。那裏沒有其他的片段。
- 上的一個按鈕的點擊,請求到Web服務時。
- 航班對象的
List
從web服務返回。 - 定製
ArrayAdapter
是在listFragment每一行創建。 - 展在前端有同樣活性的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.添加的FrameLayout到活動佈局,或其它片段佈局
<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
對象傳遞給片段。我怎樣才能做到這一點?
你加入'FlightFragment '到'MyActivity'? – 2015-02-11 12:39:44
@ρяσѕρєяK不,我正在努力與 – surfer190 2015-02-11 12:42:46
看到下面的帖子[如何添加片段的活動與程序化創建的內容視圖](http://stackoverflow.com/questions/5159982/how -do -i-add-a-fragment-to-an-activity-with-a-a-program-created-content-v)可能會幫你 – 2015-02-11 12:45:00