Android how to make EditText editable inside a ListView

前端 未结 2 397
南笙
南笙 2020-12-16 06:11

I have a ListView with edit text in each items and I want to know how can I make the EditText editable.. It seems to work fine if I set the a

2条回答
  •  悲&欢浪女
    2020-12-16 06:58

    I made a workaround for this though.. maybe will help someone

    public class EditTextListAdapter extends BaseAdapter {
    
        /* notes list */
        private ArrayList mSomeData = null;
        /* layout inflater instance */
        private LayoutInflater mInflater;
        /* activity context */
        private Context mContext;
    
        /* ensure that this constant is greater than the maximum list size */
        private static final int DEFAULT_ID_VALUE = -1;
        /* used to keep the note edit text row id within the list */
        private int mNoteId = DEFAULT_ID_VALUE;
    
        /**
         * EditTextListAdapter adapter class constructor
         *
         * @param context               activity context
         */
        public FirstTimesListAdapter(Context context) {
    
            /* get a layout inflater instance */
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            /* load the data */
            mSomeData = SomeData.instance().getData(); 
            /* keep the context */
            mContext = context;
        }
    
        /**
         * Returns the selected item
         *
         * @return selected item
         */
        public int getSelectedItem() {
            return mSelectedItem;
        }
    
        public int getCount() {
            return mSomeData.size();
        }
    
        public Object getItem(int i) {
            return mSomeData.get(i);
        }
    
        public long getItemId(int i) {
            return i;
        }
    
        public View getView(final int index, View recycledView, ViewGroup viewGroup) {
    
            ViewHolder viewHolder;
    
            if (recycledView == null) {
                /* inflate the list item */
                recycledView = mInflater.inflate(R.layout.listview_item_with_edit_text, viewGroup, false);
                /* get link to the view holder views */
                viewHolder = new ViewHolder();
                viewHolder.note = (EditText) recycledView.findViewById(R.id.first_times_note);
                recycledView.setTag(viewHolder);
            } else {
                /* reuse the same views we load the first time */
                viewHolder = (ViewHolder) recycledView.getTag();
            }
    
            /* display some notes */
            viewHolder.note.setText(mSomeData.getNotes());
    
            /* if the last id is set, the edit text from this list item was pressed */
            if (mNoteId == index) { 
    
                /* make the edit text recive focus */
                viewHolder.note.requestFocusFromTouch();
                /* make the edit text's cursor to appear at the end of the text */
                viewHolder.note.setSelection(viewHolder.note.getText().length());
    
                /* reset the last id to default value */
                mNoteId = DEFAULT_ID_VALUE;
            }
    
            /* set a touch listener on the edit text just to record the index of the edit text that was pressed */
            viewHolder.note.setOnTouchListener(new View.OnTouchListener() {
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                        /* get the index of the touched list item */
                        mNoteId = index;
                    }
                    return false;
                }
            });
    
            return recycledView;
        }
    
        static class ViewHolder {        
            /* note input */
            EditText note;
        }
    
    }
    

提交回复
热议问题