I have an adapter:

public class CustomListAdapter_Words extends BaseAdapter { private static String TAG = "myApplication"; private static String MSG = "Adapter: "; private final Activity context; private final String[][] words_form_1; private final String[][] words_form_2; public CustomListAdapter_Words(Activity context, String[][] words_form_1, String[][] words_form_2) { this.context = context; this.words_form_1 = words_form_1; this.words_form_2 = words_form_2; } static class ViewHolder_form_1 { public TextView textView_1_form_1; public TextView textView_2_form_1; public TextView textView_3_form_1; } static class ViewHolder_form_2 { public TextView textView_1_form_2; public TextView textView_2_form_2; public TextView textView_3_form_2; public TextView textView_4_form_2; public TextView textView_5_form_2; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder_form_1 holder_1; ViewHolder_form_2 holder_2; View rowView_1 = convertView; View rowView_2 = convertView; if (rowView_1 == null) { LayoutInflater inflater = context.getLayoutInflater(); rowView_1 = inflater.inflate(R.layout.row_words_form_1, null, true); holder_1 = new ViewHolder_form_1(); holder_1.textView_1_form_1 = (TextView) rowView_1.findViewById(R.id.text_1); holder_1.textView_2_form_1 = (TextView) rowView_1.findViewById(R.id.text_2); holder_1.textView_3_form_1 = (TextView) rowView_1.findViewById(R.id.text_3); rowView_1.setTag(holder_1); } else { holder_1 = (ViewHolder_form_1) rowView_1.getTag(); } if (rowView_2 == null) { LayoutInflater inflater = context.getLayoutInflater(); rowView_2 = inflater.inflate(R.layout.row_words_form_2, null, true); holder_2 = new ViewHolder_form_2(); holder_2.textView_1_form_2 = (TextView) rowView_2.findViewById(R.id.text_1); holder_2.textView_2_form_2 = (TextView) rowView_2.findViewById(R.id.text_2); holder_2.textView_3_form_2 = (TextView) rowView_2.findViewById(R.id.text_3); holder_2.textView_4_form_2 = (TextView) rowView_2.findViewById(R.id.text_4); holder_2.textView_5_form_2 = (TextView) rowView_2.findViewById(R.id.text_5); rowView_2.setTag(holder_2); } else { holder_2 = (ViewHolder_form_2) rowView_2.getTag(); } if (position < words_form_1.length) { holder_1.textView_1_form_1.setText(words_form_1[position][0]); holder_1.textView_2_form_1.setText(words_form_1[position][1]); holder_1.textView_3_form_1.setText(words_form_1[position][2]); return rowView_1; } else { holder_2.textView_1_form_2.setText(words_form_1[position][0]); holder_2.textView_2_form_2.setText(words_form_1[position][1]); holder_2.textView_3_form_2.setText(words_form_1[position][2]); holder_2.textView_3_form_2.setText(words_form_1[position][3]); holder_2.textView_3_form_2.setText(words_form_1[position][4]); return rowView_2; } } @Override public int getCount() { return words_form_1.length; } @Override public Object getItem(int position) { String[][] item = new String[1][8]; item[0][0] = words_form_1[position][0]; item[0][1] = words_form_1[position][1]; item[0][2] = words_form_1[position][2]; item[0][3] = words_form_2[position][0]; item[0][4] = words_form_2[position][1]; item[0][5] = words_form_2[position][2]; item[0][6] = words_form_2[position][3]; item[0][7] = words_form_2[position][4]; return item; } @Override public long getItemId(int position) { return position; } 

Here, at performance the error jumps out:

 if (rowView_2 == null) { LayoutInflater inflater = context.getLayoutInflater(); rowView_2 = inflater.inflate(R.layout.row_words_form_2, null, true); holder_2 = new ViewHolder_form_2(); holder_2.textView_1_form_2 = (TextView) rowView_2.findViewById(R.id.text_1); holder_2.textView_2_form_2 = (TextView) rowView_2.findViewById(R.id.text_2); holder_2.textView_3_form_2 = (TextView) rowView_2.findViewById(R.id.text_3); holder_2.textView_4_form_2 = (TextView) rowView_2.findViewById(R.id.text_4); holder_2.textView_5_form_2 = (TextView) rowView_2.findViewById(R.id.text_5); rowView_2.setTag(holder_2); } else { holder_2 = (ViewHolder_form_2) rowView_2.getTag(); } 

An error appears on the line (line 75): holder_2 = (ViewHolder_form_2) rowView_2.getTag();

stacktrace errors:

 11-03 15:50:26.301 4800-4800/? E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.ClassCastException: com.dugin_rostislav.dictionary.CustomListAdapter_Words$ViewHolder_form_1 cannot be cast to com.dugin_rostislav.dictionary.CustomListAdapter_Words$ViewHolder_form_2 at com.dugin_rostislav.dictionary.CustomListAdapter_Words.getView(CustomListAdapter_Words.java:75) at android.widget.AbsListView.obtainView(AbsListView.java:2143) at android.widget.ListView.makeAndAddView(ListView.java:1831) at android.widget.ListView.fillDown(ListView.java:674) at android.widget.ListView.fillGap(ListView.java:638) at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4930) at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3087) at android.widget.AbsListView.onTouchEvent(AbsListView.java:3361) at android.view.View.dispatchTouchEvent(View.java:7246) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953) at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405) at android.app.Activity.dispatchTouchEvent(Activity.java:2410) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901) at android.view.View.dispatchPointerEvent(View.java:7426) at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179) at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method) at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:171) at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4342) at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4382) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) at android.view.Choreographer.doCallbacks(Choreographer.java:562) at android.view.Choreographer.doFrame(Choreographer.java:530) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) 

The application itself crashes when I scroll through the ListView with this adapter, and the first 45 items (no longer fit on the screen) are loaded without failures. The error occurs ONLY when scrolling. The adapter itself receives two two-dimensional arrays of 100 elements each.

What am I doing wrong? Why does the application crash?


With this code:

  public class CustomListAdapter_Words extends BaseAdapter { private static String TAG = "myApplication"; private static String MSG = "Adapter: "; private final Activity context; private final String[][] words_form_1; private final String[][] words_form_2; public CustomListAdapter_Words(Activity context, String[][] words_form_1, String[][] words_form_2) { this.context = context; this.words_form_1 = words_form_1; this.words_form_2 = words_form_2; } static class ViewHolder_form_1 { public TextView textView_1_form_1; public TextView textView_2_form_1; public TextView textView_3_form_1; } static class ViewHolder_form_2 { public TextView textView_1_form_2; public TextView textView_2_form_2; public TextView textView_3_form_2; public TextView textView_4_form_2; public TextView textView_5_form_2; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (position < words_form_1.length) { ViewHolder_form_1 holder_1; View rowView_1 = convertView; if (rowView_1 == null) { LayoutInflater inflater = context.getLayoutInflater(); rowView_1 = inflater.inflate(R.layout.row_words_form_1, null, true); holder_1 = new ViewHolder_form_1(); holder_1.textView_1_form_1 = (TextView) rowView_1.findViewById(R.id.text_1); holder_1.textView_2_form_1 = (TextView) rowView_1.findViewById(R.id.text_2); holder_1.textView_3_form_1 = (TextView) rowView_1.findViewById(R.id.text_3); rowView_1.setTag(holder_1); } else { holder_1 = (ViewHolder_form_1) rowView_1.getTag(); } holder_1.textView_1_form_1.setText(words_form_1[position][0]); holder_1.textView_2_form_1.setText(words_form_1[position][1]); holder_1.textView_3_form_1.setText(words_form_1[position][2]); return rowView_1; } else { ViewHolder_form_2 holder_2; View rowView_2 = convertView; if (rowView_2 == null) { LayoutInflater inflater = context.getLayoutInflater(); rowView_2 = inflater.inflate(R.layout.row_words_form_2, null, true); holder_2 = new ViewHolder_form_2(); holder_2.textView_1_form_2 = (TextView) rowView_2.findViewById(R.id.text_1); holder_2.textView_2_form_2 = (TextView) rowView_2.findViewById(R.id.text_2); holder_2.textView_3_form_2 = (TextView) rowView_2.findViewById(R.id.text_3); holder_2.textView_4_form_2 = (TextView) rowView_2.findViewById(R.id.text_4); holder_2.textView_5_form_2 = (TextView) rowView_2.findViewById(R.id.text_5); rowView_2.setTag(holder_2); } else { if(rowView_2.getTag() instanceof ViewHolder_form_2) { holder_2 = (ViewHolder_form_2) rowView_2.getTag(); } else { holder_2 = new ViewHolder_form_2(); } } holder_2.textView_1_form_2.setText(words_form_1[position][0]); //92 строка holder_2.textView_2_form_2.setText(words_form_1[position][1]); holder_2.textView_3_form_2.setText(words_form_1[position][2]); holder_2.textView_3_form_2.setText(words_form_1[position][3]); holder_2.textView_3_form_2.setText(words_form_1[position][4]); return rowView_2; } } @Override public int getCount() { return (words_form_1.length + words_form_2.length); } @Override public Object getItem(int position) { String[][] item = new String[1][8]; item[0][0] = words_form_1[position][0]; item[0][1] = words_form_1[position][1]; item[0][2] = words_form_1[position][2]; item[0][3] = words_form_2[position][0]; item[0][4] = words_form_2[position][1]; item[0][5] = words_form_2[position][2]; item[0][6] = words_form_2[position][3]; item[0][7] = words_form_2[position][4]; return item; } @Override public long getItemId(int position) { return position; } @Override public int getItemViewType (int position) { return (position < words_form_1.length)?0:1; } @Override public int getViewTypeCount () { return 2; } } 

The error is this:

 11-03 19:52:20.757 1540-1540/? E/MessageQueue-JNI﹕ java.lang.ArrayIndexOutOfBoundsException: length=100; index=100 at com.dugin_rostislav.dictionary.CustomListAdapter_Words.getView(CustomListAdapter_Words.java:92) at android.widget.AbsListView.obtainView(AbsListView.java:2159) at android.widget.ListView.makeAndAddView(ListView.java:1831) at android.widget.ListView.fillDown(ListView.java:674) at android.widget.ListView.fillGap(ListView.java:638) at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4930) at android.widget.AbsListView.onGenericMotionEvent(AbsListView.java:3640) at android.view.View.dispatchGenericMotionEventInternal(View.java:7341) at android.view.View.dispatchGenericMotionEvent(View.java:7322) at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1782) at android.view.ViewGroup.dispatchGenericPointerEvent(ViewGroup.java:1735) at android.view.View.dispatchGenericMotionEvent(View.java:7315) at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1782) at android.view.ViewGroup.dispatchGenericPointerEvent(ViewGroup.java:1735) at android.view.View.dispatchGenericMotionEvent(View.java:7315) at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1782) at android.view.ViewGroup.dispatchGenericPointerEvent(ViewGroup.java:1735) at android.view.View.dispatchGenericMotionEvent(View.java:7315) at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1782) at android.view.ViewGroup.dispatchGenericPointerEvent(ViewGroup.java:1735) at android.view.View.dispatchGenericMotionEvent(View.java:7315) at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1782) at android.view.ViewGroup.dispatchGenericPointerEvent(ViewGroup.java:1735) at android.view.View.dispatchGenericMotionEvent(View.java:7315) at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1782) at android.view.ViewGroup.dispatchGenericPointerEvent(ViewGroup.java:1735) at android.view.View.dispatchGenericMotionEvent(View.java:7315) at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1782) at android.view.ViewGroup.dispatchGenericPointerEvent(ViewGroup.java:1735) at android.view.View.dispatchGenericMotionEvent(View.java:7315) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchGenericMotionEvent(PhoneWindow.java:1961) at com.android.internal.policy.impl.PhoneWindow.superDispatchGenericMotionEvent(PhoneWindow.java:1415) at android.app.Activity.dispatchGenericMotionEvent(Activity.java:2446) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchGenericMotionEvent(PhoneWindow.java:1915) at android.view.View.dispatchPointerEvent(View.java:7428) at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:125) at android.os.Looper.loop(Looper.java:124) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) 11-03 19:52:20.773 1540-1540/? E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.ArrayIndexOutOfBoundsException: length=100; index=100 at com.dugin_rostislav.dictionary.CustomListAdapter_Words.getView(CustomListAdapter_Words.java:92) at android.widget.AbsListView.obtainView(AbsListView.java:2159) at android.widget.ListView.makeAndAddView(ListView.java:1831) at android.widget.ListView.fillDown(ListView.java:674) at android.widget.ListView.fillGap(ListView.java:638) at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4930) at android.widget.AbsListView.onGenericMotionEvent(AbsListView.java:3640) at android.view.View.dispatchGenericMotionEventInternal(View.java:7341) at android.view.View.dispatchGenericMotionEvent(View.java:7322) at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1782) at android.view.ViewGroup.dispatchGenericPointerEvent(ViewGroup.java:1735) at android.view.View.dispatchGenericMotionEvent(View.java:7315) at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1782) at android.view.ViewGroup.dispatchGenericPointerEvent(ViewGroup.java:1735) at android.view.View.dispatchGenericMotionEvent(View.java:7315) at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1782) at android.view.ViewGroup.dispatchGenericPointerEvent(ViewGroup.java:1735) at android.view.View.dispatchGenericMotionEvent(View.java:7315) at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1782) at android.view.ViewGroup.dispatchGenericPointerEvent(ViewGroup.java:1735) at android.view.View.dispatchGenericMotionEvent(View.java:7315) at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1782) at android.view.ViewGroup.dispatchGenericPointerEvent(ViewGroup.java:1735) at android.view.View.dispatchGenericMotionEvent(View.java:7315) at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1782) at android.view.ViewGroup.dispatchGenericPointerEvent(ViewGroup.java:1735) at android.view.View.dispatchGenericMotionEvent(View.java:7315) at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1782) at android.view.ViewGroup.dispatchGenericPointerEvent(ViewGroup.java:1735) at android.view.View.dispatchGenericMotionEvent(View.java:7315) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchGenericMotionEvent(PhoneWindow.java:1961) at com.android.internal.policy.impl.PhoneWindow.superDispatchGenericMotionEvent(PhoneWindow.java:1415) at android.app.Activity.dispatchGenericMotionEvent(Activity.java:2446) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchGenericMotionEvent(PhoneWindow.java:1915) at android.view.View.dispatchPointerEvent(View.java:7428) at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:125) at android.os.Looper.loop(Looper.java:124) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) 
  • one
    I often see questions like yours and all the problems are stupid due to not knowing the work of the adapter and what it is for. Before you do something, read normal tutorials or offs. tutorials from google - BORSHEVIK
  • @BORSHEVIK, I didn’t find normal tutorials in Russian (I read the curves, but about the adapter’s work, what’s the reason behind it — not a word), and for Google’s tutorials I’m not very good at knowing English. - user189127 2:01

3 answers 3

Firstly - there is a convention on writing code. Java Convention and Android developer , they are strongly recommended to adhere, in particular, when naming. So, the variable name should be written in the LowCamaelCase style. Be brief, but informative.

Secondly, you have an obvious misunderstanding of what you are doing. You need to pay much more attention to the theory, then try to implement something in practice.

By code:
If you decide to use the getItemViewType() method, you should not only override it, but also use it for its intended purpose - to determine what type of item is required at the current position.
The ViewHolder pattern only stores links to widgets, it’s not worth complicating the code, creating a lot of these holders, let alone building business logic on them - the pattern is not intended for this. Moreover, even in my example, the ViewHolder class ViewHolder redundant; here it is enough to define five fields for a TextView — the first type of item will use only the first three, the second type will use all five. I also decided to rewrite it and it was absolutely lazy.
What returns your getItem() method to me failed to do, let's hope this is exactly what you need to get from this method.

This is how the adapter code should look like. Naturally, I will not create an environment to test its performance, but I hope you take into account the differences in the code and then you will be able to act independently.

 public class WordsAdapter extends BaseAdapter { private final String TAG = "myApplication"; private final String MSG = "Adapter: "; private final int FIRST_WORDS = 0; private final int SECOND_WORDS = 1; private final int FIRST_ARRAY_LENGTH; private final Activity mContext; private final String[][] mWordsForm1; private final String[][] mWordsForm2; private LayoutInflater mInflater; public WordsAdapter(Activity context, String[][] wordsForm1, String[][] wordsForm2) { mContext = context; mWordsForm1 = wordsForm1; mWordsForm2 = wordsForm2; mInflater = context.getLayoutInflater(); FIRST_ARRAY_LENGTH = mWordsForm1.length; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; int type = getItemViewType(position); if (convertView == null) { holder = new ViewHolder(); switch (type) { case FIRST_WORDS: convertView = mInflater.inflate(R.layout.row_words_form_1, null, true); holder.textView1Form1 = (TextView) convertView.findViewById(R.id.text_1); holder.textView2Form1 = (TextView) convertView.findViewById(R.id.text_2); holder.textView3Form1 = (TextView) convertView.findViewById(R.id.text_3); break; case SECOND_WORDS: convertView = mInflater.inflate(R.layout.row_words_form_2, null, true); holder.textView1Form2 = (TextView) convertView.findViewById(R.id.text_1); holder.textView2Form2 = (TextView) convertView.findViewById(R.id.text_2); holder.textView3Form2 = (TextView) convertView.findViewById(R.id.text_3); holder.textView4Form2 = (TextView) convertView.findViewById(R.id.text_4); holder.textView5Form2 = (TextView) convertView.findViewById(R.id.text_5); break; } convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } switch (type){ case FIRST_WORDS: holder.textView1Form1.setText(mWordsForm1[position][0]); holder.textView2Form1.setText(mWordsForm1[position][1]); holder.textView3Form1.setText(mWordsForm1[position][2]); break; case SECOND_WORDS: holder.textView1Form2.setText(mWordsForm2[position - FIRST_ARRAY_LENGTH][0]); holder.textView2Form2.setText(mWordsForm2[position - FIRST_ARRAY_LENGTH][1]); holder.textView3Form2.setText(mWordsForm2[position - FIRST_ARRAY_LENGTH][2]); holder.textView3Form2.setText(mWordsForm2[position - FIRST_ARRAY_LENGTH ][3]); holder.textView3Form2.setText(mWordsForm2[position - FIRST_ARRAY_LENGTH][4]); break; } return convertView; } @Override public int getCount() { return (mWordsForm1.length + mWordsForm2.length); } @Override public int getItemViewType (int position) { return (position < mWordsForm1.length) ? FIRST_WORDS:SECOND_WORDS; } @Override public int getViewTypeCount () { return 2; } @Override public Object getItem(int position) { String[][] item = new String[1][8]; item[0][0] = mWordsForm1[position][0]; item[0][1] = mWordsForm1[position][1]; item[0][2] = mWordsForm1[position][2]; item[0][3] = mWordsForm2[position][0]; item[0][4] = mWordsForm2[position][1]; item[0][5] = mWordsForm2[position][2]; item[0][6] = mWordsForm2[position][3]; item[0][7] = mWordsForm2[position][4]; return item; } @Override public long getItemId(int position) { return position; } static class ViewHolder { public TextView textView1Form1; public TextView textView2Form1; public TextView textView3Form1; public TextView textView1Form2; public TextView textView2Form2; public TextView textView3Form2; public TextView textView4Form2; public TextView textView5Form2; } } 
  • one
    You have a mistake here, which I did not notice yesterday myself, but I found the vehicle (see the comment on my answer). From the second array, the values ​​should not be taken by position, but by position-FIRST_ARRAY_LENGHT - Yuriy SPb ♦
  • 2
    @YuriiSPb Yes, for sure. - pavlofff
  • Your code has become clearer. I wrote the adapter, but did not understand part of what was written, because I didn’t know what it was, I watched the official documentation, but the phrase "this method sets a label for the object", what they said about setTag and the like didn’t say anything to me. In your code, everything is clear (well, almost everything. I still do not fully understand how the "cycle of methods and variables" goes in the adapter), clearly and ... it works! In terms of meaning, there is still a bit left, so I’ll finish the program, and then a book on Android in my hands and right ... read! - user189127
 View rowView_1 = convertView; View rowView_2 = convertView; 

crash because you have rowView_2=rowView_1 , and the type is ViewHolder_form_1 , and you are trying to cast it to the type ViewHolder_form_2 .

 if (rowView_2 == null) {...} else { (ViewHolder_form_2) rowView_2.getTag(); } 

here you rowView_2 not null , it is equal to rowView_1 , and accordingly the type of the tag there is also 1.

  • How to fix it? Cannot do new convertView . Not allowing both assignments to be performed right away - also failed. - user189127

You need to operate with holders after checking the position, not before. Simply place the second block of code in the else block (position check). Similar block of operations with holder1 - in if(position < words_form_1.length)

And the error means that you are trying to cast the object of one class to another. Why did it happen - track your code, the order of its execution and what value is assigned to which variable depending on the position.

UPD_0:

Also try adding methods to support different types of markup for elements.

 @Override public int getItemViewType (int position) { return (position < words_form_1.length)?0:1; } @Override public int getViewTypeCount () { return 2; } 

Personally, I doubt that this is the case, but it may well help.

UPD_1:

I do not see any errors in your code. Maybe they are somewhere else. I suggest that you use such a crutch - check which class comes to the argument.

 if(rowView_2.getTag() instanceof ViewHolder_form_2) { holder_2 = (ViewHolder_form_2) rowView_2.getTag(); } else { holder_2 = new ViewHolder_form_2(); } 
  • one
    Doesn't work - what does that mean? - YurySPb ♦
  • one
    Now you have another error. You are trying somewhere to refer to the hundredth (index mean) element of the array, which is a hundred elements long. Those. to a non-existent element. And again you forgot to specify a specific line on which the error got out. - Yuriy SPb ♦
  • one
    You have a copy-paste error. Instead of the second, you try to extract values ​​from the first array. Be careful. - YurySPb ♦
  • one
    You have some very strange approach ... What and why are you doing is incomprehensible. If I were you, I would take a simple adapter from scratch and then try to write data to it. What is happening at the moment is impossible to understand. - Yuriy SPb ♦
  • one
    Just lay, looked at the ceiling, thought and understood what the jamb! In the second array there are 100 elements, and position is 101. You need to position - words_form_1.lenght . Tomorrow I'll fix it, I'm already sleeping :) - user189127