2012-07-27 72 views
1

我試圖製作一個選項卡式的Android應用程序。這兩個標籤都是列表視圖。第一個標籤從RSS Feed中獲取帖子。第二個選項卡按類別顯示它們。填充從另一個片段的列表視圖

問題是我想在第一個選項卡片段的第二個選項卡中填充listview。

我的第一個片段代碼:

public class PostsFragment extends Fragment { 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.posts_fragment, container, false); 
    return view; 
} 

... 
... 
... 

// Populate the "posts_fragment.xml" with Posts 
public void populate_postview() { 
    ListView lv1; 

    lv1 = (ListView)getView().findViewById(R.id.postlist); 
    lv1.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1 , arrays.PsychoTitle)); 
    lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
      ...    
     } 
    }); 

// Populate the "categories_fragment.xml" with Categories 
public void populate_catview() { 
    ListView lv2; 

    lv2 = (ListView)getView().findViewById(R.id.catList); 
    lv2.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1 , arrays.PsychoCategories)); 
} 

這裏的問題是,在catListpopulate_catview()的「categories_fragment.xml」的一部分,但使用getView()返回當前的佈局,這是「posts_fragment.xml」 。

那麼如何填充第二個片段中的項目列表?

回答

1

假設這兩個片段具有相同的父活動,一種解決方案是將支持該列表的數據保存在父活動中。然後,當用戶輕擊任一選項卡時,相應的片段將檢索數據並相應地填充列表。