1

At the moment im looping through a dynamic arraylist and building a Listview with Switches and Edittexts from its data.

    for (int i = 0; i < response.size(); ++i) {
...
xxx.SetId(i)
...
}

this was my working solution if a Edittext was on the first position:

if (i == 0 ) {
                    editText.requestFocus();
                    getWindow().setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                }   

But how to set the focus on the first available Edittext? for example third position after two switches? (the position changes dynamically based on the arraylist)

4

1 回答 1

1

通常,您应该在附加到ListView的Adapter中填充和设置任何所需的状态。一个简单的解决方案是:

public class MyAdapter extends Adapter {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = layoutInflator.inflate(R.layout.somelayout, parent, false);
        // position is zero-based so 3rd item is at position 2
        if (position == 2) {
            view.findViewById(R.id.edittext).requestFocus();
        }
        return view;
    }
}

并将列表视图滚动到您可以调用的第三个项目listView.setSelection(2)

于 2013-08-08T22:26:02.850 回答