我的适配器有问题 class

i have a mistake in my adapter class

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_messages, container, false);


    brands.add("microsoft");
    brands.add("ios" );
    brands.add("android");
    AdapterListView adapter = new AdapterListView(brands,getActivity());

    ListView listView = (ListView)rootView.findViewById(R.id.mobile_list);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(getActivity(),
                    "selected " + i, Toast.LENGTH_LONG)
                    .show();
        }
    });

当我实际敬酒时,我只能打印位置,我还想打印品牌中的值以供选择。我怎样才能做到这一点

您使用 get 获取列表成员。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getActivity(),
                        "selected " + i + brands.get(i), Toast.LENGTH_LONG)
                        .show();
            }
});

像这样获取选定的项目字符串:

String item = (String) parent.getItemAtPosition(position);

其中 parent 是您的 AdapterView,position 是所选项目的位置,在您的情况下是 adapterView i