Home Forum

ArrayAdaptor has multiple TextViews -> Fatal Exception!

Moving from a SimpleAdapter to an ArrayAdaptor generates following exception:
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

Based on research, some .XML file is wrong, tried several variations unsuccessfully:
Code (Text):
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     tools:context="com.test.MainActivity">
  8.     <TextView
  9.         android:id="@+id/test123"
  10.         android:layout_width="match_parent"
  11.         android:layout_height="wrap_content"
  12.         android:gravity="center"
  13.         android:text="Testing 123 Title"/>
  14.     <TextView
  15.         android:id="@+id/pgroup"
  16.         android:layout_width="110dp"
  17.         android:layout_weight="1"
  18.         android:layout_height="wrap_content"
  19.         android:layout_below="@+id/test123"
  20.         android:layout_marginTop="12dp"
  21.         android:textSize="20dip"
  22.         android:textStyle="bold"
  23.         android:text="PGroup"/>
  24.     <Button
  25.         android:id="@+id/button_A"
  26.         android:layout_marginTop="25dp"
  27.         android:layout_toRightOf="@+id/pgroup"
  28.         android:layout_width="130dp"
  29.         android:layout_weight="1"
  30.         android:layout_height="wrap_content"
  31.         android:textSize="15dip"
  32.         android:textStyle="bold"
  33.         android:text="A Data"/>
  34.     <Button
  35.         android:id="@+id/button_B"
  36.         android:layout_marginTop="25dp"
  37.         android:layout_toRightOf="@+id/button_A"
  38.         android:layout_marginLeft="5dp"
  39.         android:layout_width="130dp"
  40.         android:layout_weight="1"
  41.         android:layout_height="wrap_content"
  42.         android:textSize="15dip"
  43.         android:textStyle="bold"
  44.         android:text="B Data" />
  45.     <ListView
  46.         android:id="@+id/mlist"
  47.         android:layout_width="match_parent"
  48.         android:layout_height="wrap_content"
  49.         android:layout_below="@+id/pgroup"
  50.         android:layout_marginTop="20dp"
  51.         android:gravity="center" />
  52. </RelativeLayout>
  53.  
Here is the m_list_layout.XML
Code (Text):
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="wrap_content"
  6.     android:orientation="horizontal"
  7.     android:padding="@dimen/activity_horizontal_margin">
  8.  
  9.     <!-- Body -->
  10.     <TextView
  11.         android:id="@+id/tv_pcode"
  12.         android:layout_width="40dp"
  13.         android:layout_height="16dp"
  14.         android:layout_alignParentLeft="true"
  15.         android:gravity="left"
  16.         android:textAppearance="?android:textAppearanceSmall"
  17.         android:text="p" />
  18.  
  19.     <TextView
  20.         android:id="@+id/tv_fdate"
  21.         android:layout_width="140dp"
  22.         android:layout_height="match_parent"
  23.         android:layout_toRightOf="@id/tv_pcode"
  24.         android:gravity="left"
  25.         android:textAppearance="?android:textAppearanceSmall"
  26.         android:text="feed datetime" />
  27.  
  28.     <TextView
  29.         android:id="@+id/tv_qty"
  30.         android:layout_width="55dp"
  31.         android:layout_height="match_parent"
  32.         android:layout_toRightOf="@+id/tv_fdate"
  33.         android:layout_marginLeft="5dp"
  34.         android:gravity="right"
  35.         android:textAppearance="?android:textAppearanceSmall"
  36.         android:text="fqty" />
  37.  
  38.     <TextView
  39.         android:id="@+id/tv_staff"
  40.         android:layout_width="wrap_content"
  41.         android:layout_height="match_parent"
  42.         android:layout_toRightOf="@+id/@+id/tv_qty"
  43.         android:layout_marginLeft="10dp"
  44.         android:gravity="left"
  45.         android:textAppearance="?android:textAppearanceSmall"
  46.         android:text="staff name" />
  47. </RelativeLayout>
  48.  
The offending bit of Java, apparently I cannot have more than 2 TextViews and they must be TextViews. That's just as unacceptable as the SimpleAdaptor...
Code (Text):
  1.  
  2. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  3.  
  4.     private ListView listView;
  5.     private ProgressDialog progressdialog;
  6.     private String TAG = MainActivity.class.getSimpleName();
  7.     private String link, pgroup;
  8.     // instantiate a hashmap to hold json value-pair
  9.     ArrayList<HashMap<String, String>> mList;
  10.     @Override
  11.  
  12.    protected void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.activity_main);
  15.         mList= new ArrayList<HashMap<String, String>>();
  16.         listView = (ListView) findViewById(R.id.mlist);
  17.         // ...various unrelated declarations...
  18.    }
  19.         protected void onPostExecute(Void result) {
  20.             super.onPostExecute(result);
  21.             // shutdown progress dialog
  22.             if (progressdialog.isShowing()) progressdialog.dismiss();
  23.             // stuff the parsed json from the arraylist into our listview
  24.             // ... lots of mundane code loading name-value pairs intoArray<HashMap<String,String>>
  25.  
  26.             /*** works well but has unacceptable limitations in clearing and scrolling***
  27.             ListAdapter listadapter = new SimpleAdapter(
  28.                     MainActivity.this,
  29.                     mastbeckenList,
  30.                     R.layout.mastbecken_list_layout,
  31.                     new String[]{"pcode", "fdate", "qty", "staff"},
  32.                     new int[] {R.id.tv_pcode, R.id.tv_fdate, R.id.tv_qty, R.id.tv_staff});
  33.             ***/
  34.  
  35.             ArrayAdapter<HashMap<String,String>> listAdapter = new ArrayAdapter<HashMap<String,String>> (
  36.                     MainActivity.this,
  37.                     R.layout.m_list_layout,
  38.                     mList );
  39.             Log.e(TAG, "4. ArrayAdapter error" );
  40.             // clear the adapter
  41.             listView.setAdapter(null);
  42.             // ...then plug the listview into the adapter to display the new records
  43.             listView.setAdapter(listAdapter);
  44.             //
  45.         }
  46.  

UPDATE12/7/17 4:17
Aww, gee, do I have to customize an ArrayAdaptor? Is there a less intensive way of doing it?
 

Related:

#1 AcinShn, Dec 7, 2017 at 3:00 AM Last edited: Dec 7, 2017 at 3:19 AM
Moved to Application Development forum.

Can you post the whole exception stack trace from the Logcat view please?

And yes, if you wish to create a custom row layout for your ListView, then you will need to write a custom adapter. SimpleAdapter is just a canned row layout, which in most cases does not fit your requirements.

Also, I would strongly recommend using RecylerView, in preference to ListView. RecyclerView is a more advanced version of ListView, with built in View recycling.

https://developer.android.com/guide/topics/ui/layout/recyclerview.html

If by "intensive" you mean complex, then that is the price you pay for flexibility. It's not that bad once you get familiar with the pattern.
 
#2 LV426, Dec 7, 2017 at 4:50 AM Last edited: Dec 7, 2017 at 5:02 AM
Thx for pointer re RecyclerView. Stack as requested.

Can you post the whole exception stack trace from the Logcat view please?


java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)

at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2347)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
at android.widget.ListView.onMeasure(ListView.java:1182)
at android.view.View.measure(View.java:17547)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:697)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
at android.view.View.measure(View.java:17547)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2347)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
at android.widget.ListView.onMeasure(ListView.java:1182)
at android.view.View.measure(View.java:17547)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:697)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
at android.view.View.measure(View.java:17547)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 
#3 AcinShn, Dec 7, 2017 at 7:00 PM