java.lang.StackOverflowError: stack size 8MB while Enabling/Disabling TextView#setTextIsSelectable in ListView Adapter

后端 未结 4 1480
粉色の甜心
粉色の甜心 2021-02-15 17:48

I have an activity with ListView . Displaying the TextView in each list item. Switching the properties of selected position using these methods E

4条回答
  •  Happy的楠姐
    2021-02-15 18:11

    Finally I got the solution for my problem. I removed ViewHolder pattern in MyAdapter like

    private static class MyAdapter extends BaseAdapter {
        private static final String TAG = "HistoryAdapter";
        private final LayoutInflater inflater;
        private int mSelectedPosition = -1;
        private String[] mItems;
    
        public MyAdapter(Context context, String[] mItems) {
            this.mItems = mItems;
            inflater = LayoutInflater.from(context);
        }
    
    
    
        public void setSelectedPosition(int mSelectedPosition) {
            this.mSelectedPosition = mSelectedPosition;
        }
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
    
            convertView = inflater.inflate(R.layout.selectable_text_layout, null, false);
            TextView selectableTV = (TextView) convertView.findViewById(R.id.selectableTextView);
    
            selectableTV.setText(getItem(position));
    
            if (position == mSelectedPosition) {
                Log.d(TAG, "getView() called with: " + "position = [" + position + "], selected = " + true);
                selectableTV.setTextIsSelectable(true);
                selectableTV.setSingleLine(false);
                selectableTV.setEllipsize(null);
            } else {
                Log.d(TAG, "getView() called with: " + "position = [" + position + "], selected = " + false);
                selectableTV.setTextIsSelectable(false);
                selectableTV.setSingleLine(true);
                selectableTV.setEllipsize(TextUtils.TruncateAt.END);
            }
    
            return convertView;
        }
    
    
        @Override
        public String getItem(int position) {
            return mItems[position];
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
    
        @Override
        public int getCount() {
            return mItems.length;
        }
    }
    

    But still didn't find the the reason for the problem when using ViewHolder pattern.If anybody find the reason let me know.

提交回复
热议问题