简体   繁体   English

ListView中的EditText?

[英]EditText in ListView?

I am using this code for getting the value of edittext in a list view. 我正在使用此代码来获取列表视图中edittext的值。

     View view = listview.getChildAt(position);
     EditText text = (EditText)view.findViewById(R.id.myEditBox);
     String contents = text.getText().toString();

But the problem is that when i scroll the list view ,position changes and i am getting a different result.ie if i scroll,the element at fifth position gets at the top and its position becomes 0.Is there any remedy for that? 但是问题是,当我滚动列表视图时,位置发生变化并且得到不同的结果。即,如果我滚动,第五个位置的元素将位于顶部,并且其位置变为0。是否有任何补救措施?

You will have to create an Adapter for your listview if you haven't already done that, and on your adapter when you set your views add a textwatcher and report when the text is changed to your activity or fragment. 如果还没有,则必须为列表视图创建一个适配器,并且在设置视图时在适配器上添加一个文本监视器,并在文本更改为活动或片段时报告。

On your adapter: 在适配器上:

edittext.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable s) {
                    ((MyActivity) getContext()).textChanged(s.toString());

                }
            });

On your activity 关于你的活动

public void textChanged(String s){

       //do somthing with the string
}

That way you don't have to guess the position fo your edit text 这样,您不必猜测编辑文本的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM