如何在片段活动下填充自定义列表视图(extends Fragment)

时间:2015-12-01 12:16:01

标签: java android android-fragments android-studio

我想在Fragment活动下添加一些项目的简单列表视图,这些项目正在扩展Fragment但不会ListFragment

这是我的代码:

public class ProductListFragment extends Fragment {

String[] ibizzproducts=
        {
                "Attendance",
                "GPS",
                "CCTV"
                "Website"
                "Application"

        };

ListView listView;


public static ProductListFragment newInstance(int sectionNumber) {
    ProductListFragment fragment = new ProductListFragment();
    Bundle args = new Bundle();
    args.putInt(Constants.ARG_SECTION_NUMBER, sectionNumber);
    fragment.setArguments(args);
    return fragment;
}

public ProductListFragment() {
    // Required empty public constructor
}

     @Override
     public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {

     List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

     for(int i=0;i < ibizzproducts.length;i++){
    HashMap<String, String> hm = new HashMap<String,String>();
    hm.put("txt", "Products Here : " + ibizzproducts[i]);

    aList.add(hm);
   }

   String[] from = { "Products Here" };

int[] to = { R.id.listView1};

View v = inflater.inflate(R.layout.fragment_product_list, container, false);

 // ListView list = (ListView)v.findViewById(R.id.listView1);

  listView = (ListView)v.findViewById(R.id.listView1);

   SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.fragment_product_list, from, to);

   listView.setAdapter(adapter);
    return v;

如果我在onCreateView()中使用此功能,则会给我错误:

java.lang.IllegalStateException: android.widget.ListView is not a  view that can be bounds by this SimpleAdapter

我只想添加简单的ListView,这样如果我从导航抽屉中选择Product,它应该会显示这个简单的产品项目列表。

请帮助我。 谢谢

4 个答案:

答案 0 :(得分:2)

将您的代码从listView.setAdapter(adapter);更改为list.setAdapter(adapter);

在完成复制粘贴时睁大眼睛

答案 1 :(得分:2)

下面:

listView.setAdapter(adapter);

listView对象为null,因为getActivity().findViewById用于从fragment_product_list布局访问ListView,这是Fragment的布局。

因为ListView的list对象是从fragment_product_list初始化的,onCreateView方法是从setAdapter方法返回的,所以ListView用于list.setAdapter(adapter);

{{1}}

答案 2 :(得分:1)

更好:

for(int i = 0; i < ibizzproducts.length; i++) {
    HashMap<String, String> hm = new HashMap<String,String>();
    hm.put("txt", "Products Here : " + ibizzproducts[i]);
    aList.add(hm);
}

答案 3 :(得分:1)

我希望它能帮到你

公共类FragmentShowAll扩展Fragment {

ListView listViewMsg;
ArrayList<String> array_list=new ArrayList<>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_fragment_all, container, false);

       array_list.add("hello");
       array_list.add("hello");
       array_list.add("hello");
       array_list.add("hello");
       array_list.add("hello");

    listViewMsg = (ListView) v.findViewById(R.id.listViewMsg);
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,array_list);
    listViewMsg.setAdapter(adapter);

    listViewMsg.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//on click action
        }
    });//listview on click

    return v;
}    

}