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

时间:2015-02-03 03:01:17

标签: android listview android-listview android-edittext android-listfragment

我在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;
    }

1 个答案:

答案 0 :(得分:0)

我实际上根本不应该在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;
    }