Android.widget.RelativeLayout $ LayoutParams cannot be dropped in android.widget.AbsListView $ LayoutParams

Following numerous training programs and sample projects, I came up with the following code, which should populate the ListView with strings that contain several values ​​from the Line object.

As a newbie programmer, I can't understand for life where the hell my code fails. Any help would be greatly appreciated!

If I left important information, tell me and I will update this question.

Code that runs it all inside the fragment:

@Override public void onActivityCreated(Bundle savedInstanceState){ super.onActivityCreated(savedInstanceState); Intent i = getActivity().getIntent(); if (i.getSerializableExtra("ChosenWorkorder") != null){ Workorder selectedWorkorder = (Workorder) i.getSerializableExtra("ChosenWorkorder"); ArticlesListAdapter articlesListAdapter = new ArticlesListAdapter(getActivity(), R.layout.articles_list_row, selectedWorkorder.GetAllLines()); ListView articlesList = (ListView) getActivity().findViewById(R.id.ArticlesList); articlesList.setAdapter(articlesListAdapter); } } 

Custom Array Adapter:

  public class ArticlesListAdapter extends ArrayAdapter<Line> { private List<Line> lineList; private Context context; public ArticlesListAdapter(Context context, int textViewResourceId, List<Line> objects) { super(context, textViewResourceId, objects); this.lineList = objects; this.context = context; } public int getCount() { return lineList.size(); } public Line getItem(int position) { return lineList.get(position); } public long getItemId(int position) { return position; } @SuppressLint("ViewHolder") /* Que? */ public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.articles_list_row, parent, false); TextView articleName = (TextView) rowView.findViewById(R.id.textArticleName); EditText articleAmount = (EditText) rowView.findViewById(R.id.textArticleAmount); Line articleLine = lineList.get(position); articleName.setText(articleLine.getLineArticleDescription()); articleAmount.setText(articleLine.getLineArticleAmount().toString()); return parent; } } 

Below is the LogCat log for clarification (for me this does not mean anything)

 E/AndroidRuntime(15941): FATAL EXCEPTION: main E/AndroidRuntime(15941): Process: be.mabolifting.maboserve, PID: 15941 E/AndroidRuntime(15941): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams E/AndroidRuntime(15941): at android.widget.ListView.setupChild(ListView.java:1826) E/AndroidRuntime(15941): at android.widget.ListView.makeAndAddView(ListView.java:1793) E/AndroidRuntime(15941): at android.widget.ListView.fillDown(ListView.java:691) E/AndroidRuntime(15941): at android.widget.ListView.fillFromTop(ListView.java:752) E/AndroidRuntime(15941): at android.widget.ListView.layoutChildren(ListView.java:1630) E/AndroidRuntime(15941): at android.widget.AbsListView.onLayout(AbsListView.java:2087) E/AndroidRuntime(15941): at android.view.View.layout(View.java:14860) E/AndroidRuntime(15941): at android.view.ViewGroup.layout(ViewGroup.java:4643) E/AndroidRuntime(15941): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055) E/AndroidRuntime(15941): at android.view.View.layout(View.java:14860) E/AndroidRuntime(15941): at android.view.ViewGroup.layout(ViewGroup.java:4643) E/AndroidRuntime(15941): at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1594) E/AndroidRuntime(15941): at android.view.View.layout(View.java:14860) E/AndroidRuntime(15941): at android.view.ViewGroup.layout(ViewGroup.java:4643) E/AndroidRuntime(15941): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) E/AndroidRuntime(15941): at android.widget.FrameLayout.onLayout(FrameLayout.java:388) E/AndroidRuntime(15941): at android.view.View.layout(View.java:14860) E/AndroidRuntime(15941): at android.view.ViewGroup.layout(ViewGroup.java:4643) E/AndroidRuntime(15941): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374) E/AndroidRuntime(15941): at android.view.View.layout(View.java:14860) E/AndroidRuntime(15941): at android.view.ViewGroup.layout(ViewGroup.java:4643) E/AndroidRuntime(15941): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) E/AndroidRuntime(15941): at android.widget.FrameLayout.onLayout(FrameLayout.java:388) E/AndroidRuntime(15941): at android.view.View.layout(View.java:14860) E/AndroidRuntime(15941): at android.view.ViewGroup.layout(ViewGroup.java:4643) E/AndroidRuntime(15941): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2013) E/AndroidRuntime(15941): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1770) E/AndroidRuntime(15941): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019) E/AndroidRuntime(15941): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725) E/AndroidRuntime(15941): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) E/AndroidRuntime(15941): at android.view.Choreographer.doCallbacks(Choreographer.java:574) E/AndroidRuntime(15941): at android.view.Choreographer.doFrame(Choreographer.java:544) E/AndroidRuntime(15941): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) E/AndroidRuntime(15941): at android.os.Handler.handleCallback(Handler.java:733) E/AndroidRuntime(15941): at android.os.Handler.dispatchMessage(Handler.java:95) E/AndroidRuntime(15941): at android.os.Looper.loop(Looper.java:136) E/AndroidRuntime(15941): at android.app.ActivityThread.main(ActivityThread.java:5086) E/AndroidRuntime(15941): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime(15941): at java.lang.reflect.Method.invoke(Method.java:515) E/AndroidRuntime(15941): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) E/AndroidRuntime(15941): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) E/AndroidRuntime(15941): at dalvik.system.NativeStart.main(Native Method) 
+7
android android-layout android-adapter
source share
4 answers

In getView() , change

 return parent; 

to

 return rowView; 

getView() should return a view of the row, not the parent element into which the rows are placed. The parent view is provided only as a parameter, so that inflation views can be match_parent , etc.

+8
source share

Try the following:

  LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.articles_list_row, parent, false); TextView articleName = (TextView) convertView.findViewById(R.id.textArticleName); EditText articleAmount = (EditText) convertView.findViewById(R.id.textArticleAmount); 

The dont point creates a new View :

 View rowView = inflater.inflate(R.layout.articles_list_row, parent, false); 

Instead, use the "provided" View , which refers to the item string:

 convertView = inflater.inflate(R.layout.articles_list_row, parent, false); 

And like @ lallto (another answer), change your return to return convertView; to return the current list item.

For best results, use the ViewHolder template:

http://www.vogella.com/tutorials/AndroidListView/article.html

+2
source share

Return the convertView link instead of the parent in getView and also use the ViewHolder template template to improve ListView performance:

 public View getView(int position, View convertView, ViewGroup parent) { Viewholder viewholder; if(convertView==null){ viewholder = new Viewholder(); convertView = LayoutInflater.from(context).inflate(R.layout.articles_list_row, null); viewholder.articleName = (TextView) convertView.findViewById(R.id.textArticleName); viewholder.articleAmount = (EditText) convertView.findViewById(R.id.textArticleAmount); convertView.setTag(viewholder); }else{ viewholder =(Viewholder) convertView.getTag(); } viewholder.articleName.setText(lineList.get(position).getLineArticleDescription()); viewholder.articleAmount.setText(lineList.get(position).getLineArticleAmount().toString()); return convertView; } class Viewholder { TextView articleName; EditText articleAmount; } 
+2
source share

You must choose LayoutParams depending on the parent, in your case the new LinearLayout.LayoutParams should be the new RelativeLayout.LayoutParams.

0
source share

All Articles