求教关于listview内edittext点击修改内容的问题

我现在在做一个listview,
想要实现的效果是:点击每个item一下,可以默认选中不带后缀名的内容,如点击内容为“pic.jpg”会选中“pic”,并且键盘弹出可进行内容修改

我在listview的item里放的是edittext,并在adapter里对edittext进行了setOnFocusChangeListener的监听,为的是能点击后直接选中不带后缀名的字符串内容

但问题是,运行后点击item,的确可以选中不带后缀名的字符串内容,但无法编辑,点击弹出的虚拟键盘输入内容就会报异常闪退

我不知道应该怎么做,或者说我该怎么实现这样的效果

 @Override
    public View getView( int position, View convertView, ViewGroup parent) {
        mPosition =position;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.mEditView = (EditText) convertView.findViewById(R.id.id_tv_title);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.mEditView.setText("" + mDatas.get(position));
        viewHolder.mEditView.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View arg0, boolean hasFocus) {

                if (hasFocus) {
                    viewHolder.mEditView.setSelection(0, mDatas.get(mPosition).lastIndexOf('.'));
                } else {

                }

            }
        });
        return convertView;
    }

报错如下

 08-05 11:04:02.452: E/AndroidRuntime(16670): FATAL EXCEPTION: main
08-05 11:04:02.452: E/AndroidRuntime(16670): Process: com.crow.shareupload, PID: 16670
08-05 11:04:02.452: E/AndroidRuntime(16670): java.lang.IndexOutOfBoundsException: setSpan (28 ... 28) ends beyond length 5
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1018)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:611)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:607)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.text.Selection.setSelection(Selection.java:78)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.widget.EditText.setSelection(EditText.java:92)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at com.crow.shareupload.MyAdapter$2.onFocusChange(MyAdapter.java:72)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.View.onFocusChanged(View.java:5258)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.widget.TextView.onFocusChanged(TextView.java:7921)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.View.handleFocusGainInternal(View.java:5014)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.View.requestFocusNoSearch(View.java:7724)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.View.requestFocus(View.java:7703)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.View.requestFocus(View.java:7670)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.View.requestFocus(View.java:7649)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.widget.ListView.layoutChildren(ListView.java:1746)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.widget.AbsListView.onLayout(AbsListView.java:2148)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.View.layout(View.java:15686)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.ViewGroup.layout(ViewGroup.java:4967)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.View.layout(View.java:15686)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.ViewGroup.layout(ViewGroup.java:4967)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.View.layout(View.java:15686)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.ViewGroup.layout(ViewGroup.java:4967)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.View.layout(View.java:15686)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.ViewGroup.layout(ViewGroup.java:4967)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.View.layout(View.java:15686)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.ViewGroup.layout(ViewGroup.java:4967)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2076)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1833)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1058)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5796)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.Choreographer.doCallbacks(Choreographer.java:580)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.Choreographer.doFrame(Choreographer.java:550)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.os.Handler.handleCallback(Handler.java:739)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.os.Handler.dispatchMessage(Handler.java:95)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.os.Looper.loop(Looper.java:211)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at android.app.ActivityThread.main(ActivityThread.java:5333)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at java.lang.reflect.Method.invoke(Native Method)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at java.lang.reflect.Method.invoke(Method.java:372)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
08-05 11:04:02.452: E/AndroidRuntime(16670):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

2个回答

 viewHolder.mEditView.setSelection(0, mDatas.get(mPosition).lastIndexOf('.'));//这里的mPosition不对,当你点击的时候,不是你对应item的position
 定义一个
 public MyChangeListener implements OnFocusChangeListener{
             private int mPosition=0;
            public MyChangeListener(int position){
                               this.mPosition = position;
                        }
            @Override
            public void onFocusChange(View arg0, boolean hasFocus) {

                if (hasFocus) {
                    ((EditText)arg0).setSelection(0, mDatas.get(mPosition).lastIndexOf('.'));
                } else {

                }

            }
 }

 viewHolder.mEditView.setOnFocusChangeListener(new MyChangeListener(mPosition));
suyoung21
suyoung21 回复danielinbiti: 你说的是个问题,受教了,但这段代码主要还是mDatas.get(mPosition).lastIndexOf('.')里的问题,换成((EditText) arg0).getText().toString().lastIndexOf('.')就可以了,但还是没达到我要的效果,会不停的选中主文件名无法修改内容,因为焦点的问题,这边我还需要点时间尝试修改下
4 年多之前 回复
danielinbiti
danielinbiti 回复suyoung21: 你这是监听点击事件,滚动一下mPosition 就变了,比如一屏幕有5个Item,你知道你点击的时候mPosition指向最后一个Item,点前面4个都有问题。
4 年多之前 回复
suyoung21
suyoung21 这边的mPosition是个全局变量,是从getView方法参数里传过来的啊,怎么会不对
4 年多之前 回复

我之前也遇到过,好像有人解决了
http://blog.csdn.net/eyishion/article/details/51015842

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!