我将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;
}
}
答案 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);