将可检查列表视图转换为片段

时间:2018-05-31 22:15:46

标签: android android-studio listview android-fragments

我正在尝试使用列表视图顶部的按钮弹出可检查列表视图片段。用户单击导航菜单中的项目后,将使用我的按钮和列表视图片段填充主活动。我正在调用虚拟方法'错误,即使声明中的元素。这是一些代码。

我的列表片段类:

public class ThingsManager extends Fragment {

ArrayList<String> selectedItems;
ListView checkable_list;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(layout.things_manager_fragment, container, false);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    //create an ArrayList object to store selected items
    selectedItems = new ArrayList<String>();
    //create an instance of ListView
    checkable_list.findViewById(R.id.checkable_list);
    //set multiple selection mode
    checkable_list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    String[] things_factory = {"Sports", "Politics", "Food", "Television", "Movies", "Fashion",
            "Theoretical Physics"};
    //supply data itmes to ListView
    //ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.row, R.id.things_check, things_factory);
    ArrayAdapter<String> aa = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, things_factory);
    checkable_list.setAdapter(aa);
    //set OnItemClickListener
    checkable_list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // selected item
            String selectedItem = ((TextView) view).getText().toString();
            if(selectedItems.contains(selectedItem))
                selectedItems.remove(selectedItem); //remove deselected item from the list of selected items
            else
                selectedItems.add(selectedItem); //add selected item to the list of selected items
        }
    });
}

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

列出片段布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ThingsManager">

<TextView
    android:id="@+id/things_manager_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Things Manager"
    android:textSize="25dp"/>

<!-- TODO: Update blank fragment layout -->

    <ListView
        android:id="@+id/checkable_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

我应该在MainActivity还是FragmentClass

中控制列表视图

1 个答案:

答案 0 :(得分:0)

将您的所有视图初始化和列表视图设置代码从onActivityCreated移至onCreateView方法。

你的这句话

//create an instance of ListView
checkable_list.findViewById(R.id.checkable_list);

错了。您必须从ListView中充气的视图中找到onCreateView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
    // Inflate the layout for this fragment
   View rootView =  inflater.inflate(layout.things_manager_fragment, container, false);
   init(rootView);
   return rootView;
}

public void init(View rootView){


   //create an ArrayList object to store selected items
   selectedItems = new ArrayList<String>();
   //create an instance of ListView
   checkable_list = rootView.findViewById(R.id.checkable_list);
   //set multiple selection mode
   checkable_list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
   String[] things_factory = {"Sports", "Politics", "Food", "Television", "Movies", "Fashion",
        "Theoretical Physics"};
   //supply data itmes to ListView
   //ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.row, R.id.things_check, things_factory);
   ArrayAdapter<String> aa = new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_list_item_1, things_factory);
   checkable_list.setAdapter(aa);
   //set OnItemClickListener
   checkable_list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // selected item
        String selectedItem = ((TextView) view).getText().toString();
        if(selectedItems.contains(selectedItem))
            selectedItems.remove(selectedItem); //remove deselected item from the list of selected items
        else
            selectedItems.add(selectedItem); //add selected item to the list of selected items
    }
   });

}