片段中无法设置setListAdapter

时间:2015-11-15 03:31:51

标签: android android-fragments

我是Android开发的新手,所以编程。我学习了创建列表视图的示例以及使用MyFragmentPagerAdapter创建选项卡式布局的另一个示例。但是当我尝试将这两者合并在一起时(即在列表的不同片段中创建列表视图。片段类中的“(this,generateData())”会得到红线,表示无法应用。还有“setListAdapter” “会得到红线说这个方法无法解决。

public class TabFragment1 extends Fragment  {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.tab_fragment_1, container, false);
        return view;
    }
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        // 1. pass context and data to the custom adapter
        MyAdapter adapter = new MyAdapter(this, generateData());

        //2. setListAdapter
        setListAdapter(adapter);
    }

    private ArrayList<DrugItem> generateData(){
        ArrayList<DrugItem> items = new ArrayList<DrugItem>();
        items.add(new DrugItem("Item 1","First Item on the list"));
        items.add(new DrugItem("Item 2","Second Item on the list"));
        items.add(new DrugItem("Item 3","Third Item on the list"));
        items.add(new DrugItem("Item 1","First Item on the list"));
        items.add(new DrugItem("Item 2","Second Item on the list"));
        items.add(new DrugItem("Item 3","Third Item on the list"));
        items.add(new DrugItem("Item 1","First Item on the list"));
        items.add(new DrugItem("Item 2","Second Item on the list"));
        items.add(new DrugItem("Item 3","Third Item on the list"));
        items.add(new DrugItem("Item 1","First Item on the list"));
        items.add(new DrugItem("Item 2","Second Item on the list"));
        items.add(new DrugItem("Item 3","Third Item on the list"));
        return items;
    }
}

1 个答案:

答案 0 :(得分:0)

setListAdapter()ListFragment的方法,而非Fragment。让你的课程扩展ListFragment而你应该没事。