1

I want to populate items without using a xml layout instead I want to create view at the runtime.This is my getView() method.

 public View getView(final int position, View convertView, ViewGroup parent) {

    EditText v ;    

    if(convertView ==null)
    {
        v = new EditText(mContext);
        convertView = v;
        convertView.setTag(R.string.edittext_child, v);

    }
    else
    {
        v = (EditText)convertView.getTag(R.string.edittext_child);
    }
    v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 80));
    v.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.custom_edit));
    v.setEms(12);
    v.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
    v.setPadding(10, 0, 5, 0);
    v.setHint(Ids[position]);
    return convertView;
}

I am getting this error while returning my view.

java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
Mihir
  • 2,064
  • 2
  • 21
  • 28

2 Answers2

1

Right now propably you've imported the LinearLayout Layout Params which is a wrong import. Thats why you're getting

java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

Because you're trying to add Linear Layout params to AbsList Layout which should be filled with AbdList Params

This line is wrong:

v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 80));

Change LayoutParams to :

AbsListView.LayoutParams

or change your import:

import android.widget.AbsListView;
Klawikowski
  • 605
  • 3
  • 11
1

Use AbsListView.LayoutParams

 v.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, 80));
Arun C
  • 9,035
  • 2
  • 28
  • 42