I have an activity with ListView
. Displaying the TextView
in each list item. Switching the properties of selected position using these methods E
i was calling onBackPressed()
recursively and that caused StackOverFlowError
for me.
To much of iteration on list scroll will produce this error.. Avoid that...
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.
The best way to find the error is use a lot of logcat
in your code and find where the logcat
is not showing.
But you can try to change your code like this:
inflater = LayoutInflater.from(context);
in MyAdapter constructormove it in getview
method like this:
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView=inflater.inflate(R.layout.selectable_text_layout, null,false);</br>
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}