Android / Java - 如何找到 listView 中包含微调器的行

Android / Java - How can I find the row in which a spinner is contained in a listView

我有一个 listView,其中每一行都包含一个 textView 和一个 Spinner。我可以同时填充两者,而且我可以毫无问题地从 Spinner select。当用户 selects 来自微调器时,我可以使用 onItemSelected() 读取 selection。

我的问题是我不知道如何识别 listView 中的哪一行被 selected。

我试过使用 setTag() 和 setId(),但它们中的任何一个都无法满足我的需求。

我还尝试在该行上放置一个透明按钮并使用 onClickListener,但该按钮遮盖了微调器,因此您实际上并没有激活微调器。

下面是我的代码。如果能提供正确方向的任何帮助或指示,我将不胜感激。

我在 SO 上查看了数十篇相关帖子,但找不到真正回答这个问题的帖子。抱歉,如果我错过了。

谢谢

适配器中 getView() 的代码。

public class AdapterSelectEventPayment extends BaseAdapter {

    private ArrayList<String> mSpinnerItems;
    private ArrayList<String> mDataSource;

    private Context mContext;
    private AdapterView.OnItemSelectedListener mSpinnerItemSelectedListener;

    public AdapterSelectEventPayment(ArrayList<String> data, ArrayList<String> spinnerItems, Context context) {
        mDataSource = data;
        mContext = context;
        mSpinnerItems = spinnerItems;
    }

    @Override
    public int getCount() {
        return mDataSource.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataSource.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.select_event_charge_items, null);
        }

        TextView textView = (TextView) view.findViewById(R.id.row_item_textview);
        Spinner spinner = (Spinner) view.findViewById(R.id.row_item_spinner);

        textView.setText(mDataSource.get(position));
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_dropdown_item, mSpinnerItems);
        spinner.setAdapter(adapter);
        spinner.setTag(position);

        spinner.setOnItemSelectedListener(mSpinnerItemSelectedListener);

        return view;
    }

    public void setOnSpinnerItemSelectedListener(AdapterView.OnItemSelectedListener listener){
        this.mSpinnerItemSelectedListener = listener;
    }

}

主要代码Activity

private void sortArrayList() {
        
            AdapterSelectEventPayment adapter = new AdapterSelectEventPayment(participants, chargeArray, this);

            adapter.setOnSpinnerItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                    System.out.println("The spinner has been spun... and position is " + position + " and the id = " + id);
                    System.out.println("Tag is " + selectedItemView.getTag());
                }

                @Override
                public void onNothingSelected(AdapterView<?> parentView) {
                    System.out.println("The spinner has NOT been spun...");
                }
            });

            lvSpinner.setAdapter(adapter);
    }

我建议您创建一个界面,用来自微调器选择的所有相关信息 + 所点击项目的位置给您回电

interface ListSpinnerListener() {
    void onSpinnerItemSelected(int rowPos, int selectedItemPos, ...);
}

您的 Activity 可以按照您分配 AdapterView.OnItemSelectedListener 的方式进行分配,而是直接在您内部使用该侦听器 getView() 方法

适配器:

private ListSpinnerListener listSpinnerListener;

...

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.select_event_charge_items, null);
    }

    TextView textView = (TextView) view.findViewById(R.id.row_item_textview);
    Spinner spinner = (Spinner) view.findViewById(R.id.row_item_spinner);

    textView.setText(mDataSource.get(position));
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_dropdown_item, mSpinnerItems);
    spinner.setAdapter(adapter);
    spinner.setTag(position);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int itemPosition, long id) {
            System.out.println("The spinner has been spun... and position is " + position + " and the id = " + id);
            System.out.println("Tag is " + selectedItemView.getTag());
            listSpinnerListener.onSpinnerItemSelectedListener(position, itemPosition, ...);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            System.out.println("The spinner has NOT been spun...");
        }
    });

    return view;
}

这样一来,您就可以确保通过一个简单的界面始终获得所需的所有相关信息,而不必通过标签等方式破解答案。

不是在 selectedItemView 上调用 getTag,而是在 parentView

上调用它