2015-02-11 26 views
0

上下文是:如何將CustomListAdapter連接到Activity中的List Fragment?

  1. 我具有由其中存在的形式輸入一個日期和一個航班號的片段的活性。那裏沒有其他的片段。
  2. 上的一個按鈕的點擊,請求到Web服務時。
  3. 航班對象的List從web服務返回。
  4. 定製ArrayAdapter是在listFragment每一行創建。
  5. 展在前端有同樣活性的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(); 
    } 
} 

那麼,我沒有得到的是如何顯示上MyActivityFlightFragment。 此外如何連接它所以結果顯示:我認爲使用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對象傳遞給片段。我怎樣才能做到這一點?

+1

你加入'FlightFragment '到'MyActivity'? – 2015-02-11 12:39:44

+0

@ρяσѕρєяK不,我正在努力與 – surfer190 2015-02-11 12:42:46

+1

看到下面的帖子[如何添加片段的活動與程序化創建的內容視圖](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

回答

1

我怎樣才能通過List對象的片段?

因爲List包含Flight自定義類對象。所以不可能直接傳遞它。

要傳遞List<Flight>對象:

1。實施SerializableFlight類。

2.使用Bundle.putSerializable從活動發送對象FlightFragment

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

FlightFragment類重寫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); 
} 
+0

非常好,它的作品就像一個魅力。謝謝。如果您認爲有一個更簡單或更優雅的方式來完成整個任務,請發表評論。 – surfer190 2015-02-11 15:25:01

+0

@StevieG:currenlty我知道的方式,我告訴我的答案謝謝 – 2015-02-11 15:28:43

相關問題