如何在tab片段中添加listview

时间:2013-02-16 07:33:31

标签: android

我创建了三个片段来显示我的主要活动。这是三个片段活动之一:

public static class Fragment1 extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    String[] values=new String[]{"India", "java", "c++","Ad.Java", "Linux", "Unix"};
    public Fragment1() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Create a new TextView and set its text to the fragment's section
        // number argument value.
        View v = inflater.inflate(R.layout.center, null);
        return v;
    }
}

我想在这个片段中显示一个listView。我试图在onCreateView()方法中使用此代码:

ArrayAdapter<String> adapter=new ArrayAdapter<String>(Main.this,android.R.layout.simple_list_item_1,values);
        setListAdapter(adapter);

但它显示错误..怎么做?

1 个答案:

答案 0 :(得分:5)

这段代码不好帮助你..

   String[] values=new String[]{"India", "java", "c++","Ad.Java", "Linux", "Unix"};
   ListView lv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.center, container, false);
perform(v);

    return v;
}

public void perform(View v) {
lv = (ListView)v.findViewById("your view id");
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,values);
    lv.setAdapter(adapter);

}