2012-07-27 76 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

假设这两个片段具有相同的父活动,一种解决方案是将支持该列表的数据保存在父活动中。然后,当用户轻击任一选项卡时,相应的片段将检索数据并相应地填充列表。