listView.setOnClickListner无法正常工作

时间:2016-02-27 10:30:39

标签: listview android-arrayadapter onclicklistener

我将onClickListener实现为像这样的listview,但没有toast显示此代码有什么问题?在listviews上实现clickListners的另一种方法?

    public class DrawerFragment extends Fragment{

    String[] drawerList = {"A", "B", "C", "D"};

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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_drawer, container, false);

        List<String> listItems = new ArrayList<>(Arrays.asList(drawerList));
        ArrayAdapter<String> listItemsAdapter = new ArrayAdapter<>(getActivity(), R.layout.drawer_list_item, R.id.drawer_list_item, listItems);

        ListView listView = (ListView) view.findViewById(R.id.drawer_list);
        listView.setAdapter(listItemsAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(), "position"+position, Toast.LENGTH_LONG).show();            }
        });

        return view;
    }


}

1 个答案:

答案 0 :(得分:0)

<强>替换

List<String> listItems = new ArrayList<>(Arrays.asList(drawerList));

。通过

List<String> listItems = new ArrayList<String>(Arrays.asList(drawerList));

<强>替换

ArrayAdapter<String> listItemsAdapter = new ArrayAdapter<>(getActivity(), R.layout.drawer_list_item, R.id.drawer_list_item, listItems);

。通过

ArrayAdapter<String> listItemsAdapter = new ArrayAdapter<String>(getActivity(), R.id.drawer_list_item, listItems);