android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

风流意气都作罢 提交于 2019-12-05 03:34:50

In your getView(), change

return parent;      

to

return rowView;

getView() should return the row view and not the parent where the rows are placed in. The parent view is only supplied as a paramteter so that view inflation can deal with match_parent sizes and such.

Try this :

  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 point is dont create a new View :

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

Instead use the "provided" View that refer to the item's row :

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

And like @lallto (other answer) said, change your return to return convertView; for returning the current list item.

For better result, use ViewHolder pattern :

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

Return convertView reference instead parent in getView and Also use ViewHolder design pattern 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;
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!