1

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

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);

但它显示错误..如何做到这一点?

4

1 回答 1

6

这段代码对你有帮助..

   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);

}

于 2013-02-16T07:46:02.260 回答