我的适配器类有错误

时间:2015-09-16 06:39:34

标签: android android-fragments android-listview android-toast

 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();
        }
    });

当我实际上敬酒我只能打印位置时,我也想打印品牌中的值以供选择。我可以这样做

2 个答案:

答案 0 :(得分:0)

使用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();
            }
});

答案 1 :(得分:0)

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

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

父级是您的AdapterView,位置是所选项目的位置,在您的情况下 adapterView i < /强>