1

Good Afternoon,

I have asked this question before with other adapters like SimpleAdapter but the solution for the simple adapter is not working for ArrayAdapter. In a SimpleAdapter I got the screen width & height then passed that to my RelativeLayout for the data row. The effect was that only one record filling the entire screen was shown at a time. That was fine with static data but now I want to use mutable data. In short I really don't want to pass the height and width at all. My xml layout is using dip so the screen sizes can change all they want. What I am REALLY after is a way to control the getView call to just one record at a time.

Anyway using the workaround from SimpleAdapter I thought passing the height and width just like before would work but for some reason this time I'm getting an error I can't figure out. Here is the code that sets the width and height:

RelativeLayout.LayoutParams szParms = new RelativeLayout.LayoutParams(mWidth, mHeight);
vw_BigRow.setLayoutParams(szParms);

The height and width are determined elsewhere and are accurate across all devices I've tested with. Not really sure what else to post so just tell me and I add that code / info to the question.

E/AndroidRuntime(404): FATAL EXCEPTION: main
E/AndroidRuntime(404): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams

Edit: As has been correctly pointed out my error occurs with a CastClassException however I am still not doing the assignment right as I continue to get the error. I have tried in the activity to set the height / width with the following:

AbsListView.LayoutParams szParms = new AbsListView.LayoutParams(mWidth, mHeight);
lstvw_LiftData.setLayoutParams(szParms);

E/AndroidRuntime(1886): FATAL EXCEPTION: main
E/AndroidRuntime(1886): java.lang.ClassCastException: android.widget.AbsListView$LayoutParams

and

ListView.LayoutParams szParms = new ListView.LayoutParams(mWidth, mHeight);
lstvw_LiftData.setLayoutParams(szParms);

E/AndroidRuntime(1886): FATAL EXCEPTION: main
E/AndroidRuntime(1886): java.lang.ClassCastException: android.widget.AbsListView$LayoutParams

the lstvw_LiftData is a valid ref and not null.

Inside the getView() override of the ArrayAdapter I also try to set the values with the same result @Override public View getView(int position, View convertView, ViewGroup parent) {...}

The only thing I can get to work is this and then a couple, Not All, but a couple of my elements are ignoring their layout positioning and going straigtht to the bottom.

View vw_BigRow = convertView;

if (vw_BigRow == null) 
{
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    vw_BigRow = inflater.inflate(R.layout.liftdatalayout, null);
}

vw_BigRow.setMinimumHeight(mHeight);
vw_BigRow.setMinimumWidth(mWidth);
GPGVM
  • 4,659
  • 8
  • 50
  • 90

1 Answers1

1

1.If you want call to just one record at a time, just control the getCount of the adapter you implemented to return 1.

2.The java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams exception is because the view want added to the listview should use AbsListView.LayoutParams instead of other LayoutParams such as RelativeLayout$LayoutParams.

EDIT:

See below getView code which is recommended by google.

/**
 * Make a view to hold each row.
 *
 * @see android.widget.ListAdapter#getView(int, android.view.View,
 *      android.view.ViewGroup)
 */
public View getView(int position, View convertView, ViewGroup parent) {
    // A ViewHolder keeps references to children views to avoid unneccessary calls
    // to findViewById() on each row.
    ViewHolder holder;

    // When convertView is not null, we can reuse it directly, there is no need
    // to reinflate it. We only inflate a new View when the convertView supplied
    // by ListView is null.
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item_icon_text, null);

        // Creates a ViewHolder and store references to the two children views
        // we want to bind data to.
        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.text);
        holder.icon = (ImageView) convertView.findViewById(R.id.icon);

        convertView.setTag(holder);
    } else {
        // Get the ViewHolder back to get fast access to the TextView
        // and the ImageView.
        holder = (ViewHolder) convertView.getTag();
    }

    // Bind the data efficiently with the holder.
    holder.text.setText(DATA[position]);
    holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);

    return convertView;
}

static class ViewHolder {
    TextView text;
    ImageView icon;
}
dreamtale
  • 2,817
  • 1
  • 23
  • 36
  • @user1278561 please just put all your getview code or all adapter code. It will help me to find the real problem. And what's your item layout? A layout or just a view. If it's just a view, you can just put the view on the root of the XML. – dreamtale May 30 '12 at 11:18
  • Thanks for the update. I will read / improve on bus during commute this morning and post more code once at work. Also overriding getCount as you suggest inhibits me from advancing to the next record...I'm sure I'm doing it wrong. I posted an unanswered question here about it. http://stackoverflow.com/questions/10693112/advance-listview-next-item-setselectionfromtop-not-working-inhibit-user-scro – GPGVM May 30 '12 at 12:39