自定义ListView与Spinner

时间:2015-11-08 08:45:22

标签: android android-listview onitemselectedlistener

====更新了====

代码块中有一个拼写错误,所以让你们感到困惑。我想要做的是获取列表视图列表项的id,它包含我现在点击的微调器。

====更新了====

我正在实现一个自定义列表视图,其中包含每个列表项的微调框和文本框。我想在列表视图的列表项中使用onItemSelected()保存微调器的选定值,但我不知道找到列表项。我怎么能得到这个,有人可以帮忙吗?

以下是列表视图适配器中的代码。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View listItemView = convertView;

    if (listItemView == null) {
        listItemView = inflater.inflate(R.layout.listitem, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.view1 = (Spinner) listItemView.findViewById(R.id.view1);
        viewHolder.view2 = (TextView) listItemView.findViewById(R.id.view2);

        viewHolder.view1.setFocusable(false);
        viewHolder.view2.setFocusable(false);

        viewHolder.view1.setAdapter(view1Adapter);
        viewHolder.view1.setPrompt("Select");
        viewHolder.view1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedId = spinnerItemList.get(position).id;
                // todo : need to find a way to get list item of list view, which has this spinner.
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        listItemView.setTag(viewHolder);
    }

    CustomListItem item = listViewItemList.get(position);
    ViewHolder viewHolder = listItemView.getTag();
    viewHolder.view1.setSelection(item.view1Value);
    viewHolder.view2.setText(String.valueOf(item.view2Value));

    return listItemView;
}

2 个答案:

答案 0 :(得分:0)

尝试这种方式它会起作用..

String selectedId = spinnerItemList.getSelectedItemPosition();

答案 1 :(得分:0)

您可以使用以下代码段找到所选项目。

viewHolder.view1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {elo

    //here if you use single Field then belo line is true to get id.

            String selectedId = spinnerItemList.get(position);

    //if you using pojoclass for data set & get then you need to do like below.

            String selectedId = spinnerItemList.get(position).getId();
    // todo : need to find a way to get list item index.


        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });