如何指向在 Android 的 ListView 中动态生成的 EditText 元素?

How can I point to EditText elements that are generated dynamically in a ListView in Android?

我正在 ListFragment 中创建一个 ListView,它正在动态填充,但我不知道那里将有多少项目。现在我需要用户为 ListView 中的每个项目设置 EditTexts 的文本,以便通过单击一个按钮来保存该文本。我的问题是,当布局中实际上只有一个 EditText 时,我如何指向每个项目的 EditText。我的代码只是将第一个 EditText 文本保存在列表中。有人有想法吗?对不起!我是 Android.

的新手
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder viewHolder;
        // General ListView optimization code.
        if (view == null) {
            view = mInflater.inflate(R.layout.listitem_device, null);
            viewHolder = new ViewHolder();
            viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
            viewHolder.deviceName = (EditText) view.findViewById(R.id.device_name);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        BluetoothDevice device = mLeDevices.get(i);
        viewHolder.deviceAddress.setText(device.getAddress());

        saveNode.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                for (int j = 0; j < mLeDeviceListAdapter.getCount() ; j++) {
                    String name = viewHolder.deviceName.getText().toString();
                    String address = viewHolder.deviceAddress.getText().toString();
                    handler = new DataHandler(getActivity().getBaseContext());
                    handler.open();
                    handler.insertNode(address, name);
                    Toast.makeText(getActivity().getBaseContext(), "Node " + name + " inserted", Toast.LENGTH_LONG).show();
                    handler.close();
                }
            }
        });

        return view;
    }

我实际上根本不应该在 getView() 中使用按钮 onClick 方法。它应该像这里一样在 onCreate 中:

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

    mHandler = new Handler();
    saveNode = (Button) rootView.findViewById(R.id.save_node_button);

    saveNode.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            for (int j = 0; j < mLeDeviceListAdapter.getCount() ; j++) {

                int wantedPosition = j; // Whatever position you're looking for
                int firstPosition = mLeDeviceListView.getFirstVisiblePosition() - mLeDeviceListView.getHeaderViewsCount(); // This is the same as child #0
                int wantedChild = wantedPosition - firstPosition;
                // Say, first visible position is 8, you want position 10, wantedChild will now be 2
                // So that means your view is child #2 in the ViewGroup:
                if (wantedChild < 0 || wantedChild >= mLeDeviceListView.getChildCount()) {
                  //Log.w(TAG, "Unable to get view for desired position, because it's not being displayed on screen.");
                  return;
                }
                // Could also check if wantedPosition is between listView.getFirstVisiblePosition() and listView.getLastVisiblePosition() instead.
                View wantedView = mLeDeviceListView.getChildAt(wantedChild);

                nodeName = (EditText) wantedView.findViewById(R.id.device_name);
                nodeAddress = (TextView) wantedView.findViewById(R.id.device_address);

                name = nodeName.getText().toString();
                address = nodeAddress.getText().toString();

                handler = new DataHandler(getActivity().getBaseContext());
                handler.open();
                handler.insertNode(address, name);
                Toast.makeText(getActivity().getBaseContext(), "Node " + name + " inserted", Toast.LENGTH_LONG).show();
                handler.close();
             }
        }
    });
    return rootView;
}

getView() 函数应该如下所示:

@Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder viewHolder;
        // General ListView optimization code.
        if (view == null) {
            view = mInflater.inflate(R.layout.listitem_device, null);
            viewHolder = new ViewHolder();
            viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
            viewHolder.deviceName = (EditText) view.findViewById(R.id.device_name);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        BluetoothDevice device = mLeDevices.get(i);
        viewHolder.deviceAddress.setText(device.getAddress());
        return view;
    }