在众多教程和示例项目之后,我得到了以下代码,该代码应该在ListView中填充包含Line对象中多个值的行.
作为一个新手程序员,我不能为我的生活弄清楚我的代码在哪里失败.任何帮助将非常感激!
如果我遗漏了重要信息,请告诉我,我会更新这个问题.
在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 ArrayAdapter:
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; } }
下面是LogCat日志以便澄清(对我而言,这意味着什么).
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)
解决方法
在你的getView()中,改变
return parent;
至
return rowView;
getView()应返回行视图而不是放置行的父级.父视图仅作为参数提供,以便视图膨胀可以处理match_parent大小等.